Pointers Flashcards
what is a pointer
is a variable whose value is a memory address
A memory address is just an integer value
Why do we need pointers?
for the sake of space and
time efficiency.
e.g, an array of 1000 elements, instead passing it to a function by value,automatic allocation , which may use up all the stack space.
pass
So the address stored in the pointer variable
would be passed by value, and thus copied into the function’s stack frame
they are the only way we can create more complex data
structures in C such as linked lists.
- for types
type*
int main () {
int * pointerToInteger ;
float * pointerToFloat ;
double * pointerToDouble ;
return 0;
}
this is not a unary Application
pointer to type
Storage Allocation for Pointer Variables
A pointer defined as a local variable gets automatically allocated
space for its address, and let’s say that it is initialized to the address 424242. However, the
memory at address 424242 might have been allocated with any allocation method
&
Used to get the memory address of a variable.
this is an unary application
int main () {
int ultimateAnswer = 42
int* pointerToUltimate ;
pointerToUltimate = &ultimateAnswer ;
return 0;
}
deferencing
the process of “following” a pointer and obtaining or changing the value in the memory location to which it “points”.
The dereferencing operator in C is the unary * operator
Using Dereferencing to Obtain the Value at the Referenced Memory Location
Using Dereferencing to Modify the Value at the Referenced Memory Location
difference in assignments
Precedence