Comp Sci 8.2 Pointer Basics
Output the address of the double variable sensorVal. printf("%p", (void*) __________);
&sensorVal
Pointer
a variable that holds a memory address, rather than holding data like most variables -A pointer has a data type, and the data type determines what type of address is held in the pointer. Ex: An integer pointer holds a memory address of an integer, and a double pointer holds an address of a double. A pointer is declared by including * before the pointer's name. Ex: int* maxItemPointer declares an integer pointer named maxItemPointer.
Refer to the code below. char userLetter = 'B'; char* letterPointer; What line of code makes letterPointer point to userLetter?
letterPointer = &userLetter; letterPointer is assigned with the address of userLetter by using the reference operator &.
char* valPointer1, *valPointer2; valPointer1 = NULL; valPointer2 = NULL;
no errors valPointer1 and valPointer2 are both declared as char pointers because both variables have asterisks preceding the variable names. However, good practice is to declare pointers on separate lines.
reference operator (&)
obtains a variable's address. Ex: &someVar returns the memory address of variable someVar. When a pointer is initialized with another variable's address, the pointer "points to" the variable.
Refer to the code below. char userLetter = 'B'; char* letterPointer; What line of code assigns the variable outputLetter with the value letterPointer points to?
outputLetter = *letterPointer; The dereference operator * gets the value letterPointer points to. If letterPointer points to userLetter, then outputLetter is now 'B'.
dereference operator (*)
prepended to a pointer variable's name to retrieve the data to which the pointer variable points. Ex: If valPointer points to a memory address containing the integer 123, then printf("%d", *valPointer); dereferences valPointer and outputs 123.
Refer to the code below. char userLetter = 'B'; char* letterPointer; What does the code output? letterPointer = &userLetter; userLetter = 'A'; *letterPointer = 'C'; printf("%c", userLetter);
C letterPointer points to userLetter, so changing *letterPointer to 'C' also changes userLetter to 'C'.
Declare a double pointer called sensorPointer.
double* sensorPointer; The asterisk * must be placed next to the variable name to make the variable a pointer.
char* newPointer; *newPointer = 'A'; printf("%d", *newPointer);
runtime error newPointer contains an unknown address when declared and is not initialized. Dereferencing an uninitialized pointer may cause the program to crash.
char* newPointer = NULL; char someChar = 'A'; *newPointer = 'B';
runtime error newPointer is initialized with NULL when dereferenced and assigned 'B'. Dereferencing a null pointer causes the program to crash.
Assign sensorPointer with the variable sensorVal's address. In other words, make sensorPointer point to sensorVal.
sensorPointer = &sensorVal; The reference operator & obtains a variable's address. Pointers are often initialized with a variable's address.
char someChar = 'Z'; char* valPointer; *valPointer = &someChar;
syntax error A pointer cannot be assigned with an address when using the dereference operator *. The correct way to assign valPointer: valPointer = &someChar;