C Pointers Flashcards
What is a pointer?
A pointer is a data type that contains a memory location
We can use a pointer to indirectly get to the values of variables.
When we say pointers have two values, what values are we referring to?
The address to stored by the pointer
The value at the address where the pointer points
What is the ‘&’ pointer operator?
Used to give the memory address of a variable
What is the ‘*’ pointer operator?
Used to tell us to look through the pointer to the underlying data
Does a pointer declaration creates space for the data?
NO!!!
What is ‘pass by reference’
Declare a function parameter to be a pointer to the variable in the calling function.
foo( int *a ) {
a = 10; / Sets the variable in the caller to 10 */
}
main() {
int b;
foo( &b ); /* b returns with the value 10 */ }
Basic Pointers
Declaration
int *p;
void *q;
Dereferencing
int *p;
*p = 7;
Assigning to a pointer from a regular variable int *p;
int a;
p = &a;
A value of NULL (from stdlib.h) is used for a non-existent memory address