#include <stdio.h>
/* A pointer, also called an address, is sometimes called a "reference". When you have a pointer to a variable(a reference), you can use the original variable through the pointer by "dereferencing" the pointer. */
/* what does "get access to the original variable" mean. Lets say you have a var called i, and you have a pointer to i called p. You can use the dereferenced pointer p exactly as if it were the original variable i */
/* The dereference operator is the asterisk, (*). Its the same character when declaring a pointer, but they have different meanings in different contexts.*/int main(void){ int i; int *p; // this is not a dereference, this is an int pointer
p = &i; // p now points to i, ie it has the address to i in memory *p = 20; // i is now 20
printf("i is %d\n", i); // prints 20 printf("i is %d\n", *p); // also prints 20, as the dereferenced p is the same as i
return 0;}
/* Remember that p holds the address of i, as you can see where we did the assignment to p. What the dereference operator does is tells the computer to use the variable the pointer points to instead of using the pointer itself. In this way, we have turned *p into an alias of sorts for i. */Note
C: Dereferencing
Using the dereference operator to access the original variable through a pointer.
views 0