Lecture 10 Flashcards
POINTERS -cont
______ alerts compile that the contents of a variable or an array will not be changed by
the program
const
Can a pointer be declared as a const
Yes, A const pointer is a pointer whose address can not be changed after initialization
If the pointer variable is always set pointing to c?
char c = ‘X’;
char *charPtr = &c;
char * const charPtr = &c;
If, the contents within the location pointed to by charPtr will not change through the pointer variable charPtr
char c = ‘X’;
char *charPtr = &c;
const char *charPtr = &c;
If the pointer variable is always set pointing to c?
char c = ‘X’;
char *charPtr = &c;
char * const charPtr = &c;
if both the pointer variable itself and the contents of the location it points to will not be changed through the pointer.
char c = ‘X’;
char *charPtr = &c;
const char * const charPtr = &c;
Write the following statement:
You have an array of 100 integers called values and want to define a pointer variable called valuesPtr, to access the integers contained in this array
int values[100];
int *valuesPtr
you must designate the pointer as pointing to the type of element that is contained in the array when ___
you define a pointer that is used to point to the elements of an array
To set valuesPtr to point to the
first element in the values array,
you simply write
valuesPtr = values
When do you not use the address operator
When the name of the
array without an address
operator is a pointer to the first
element of the array
When we’re passing arrays to functions, why isn’t a copy being made?
because we are actually passing the pointer to the array
An equivalent way of producing a pointer to the start of values is to apply the address operator to the first element of the array
valuesPtr =&values[0]
Are the following statements equivalent:
valuesPtr[i] = 3; to values[i] = 3;
Yes, because after the initialization, (valuesPtr) points to the start of the array (values) and therefore, we can sequence through the elements of the array with the pointer variable as we would just as with the array itself
Would (values) be considered a Pointer?
Yes, because the compiler gives you a const pointer variable called (values) after declaring the int array (values), that is pointing to the address of the first element of the array (the start of the array)
Why must we use a pointer to (const char)
Because string literals are constants (in the initialized data section)