Here is an example how pointers work.
- int * a; // define integer type pointer(int *) a
- int b = 5;
- a = &b; // & is get the address of the variable
- int d = *a; // * is same as int d = b
- Define the integer type pointer named a (In C, a is an int pointer which is stored in address 201)
- Define integer b and assign the value 5 to b (In C, b is 5 which is stored in address 301)
- a is pointing at the address of b (Now, a has the value of b’s address)
- * means getting the value of what a is pointing at. That is, a is pointing at b and the value of b is 5. Therefore, *a is 5. 5 is then assigned to d.
Here is the diagram of the example:
I will explain why pointers are
important and useful later.
No comments:
Post a Comment