Declaring (Initializing) Pointers
As we learned in the previous lesson, a pointer contains the address of a variable. Different types of variables have different types of pointers. For example, an integer variable will have an integer pointer; a float variable will have a float pointer.
Pointers can be variables too. You can declare a pointer at the beginning of a program, just like you declare a regular variable. There are three things you must do:
- specify the type of pointer (int, float, etc.)
- use a star (asterisk) * in front of the variable name, to show that it’s a pointer
- set the variable value at NULL, so it can be filled in later
Put these three things together, and you can declare a pointer, like this:
Now let’s try a program that declares a pointer, then uses it to show the pointer’s address:
#include<stdio.h> void main( ) { int year; int *point = NULL; printf(“What year is it? “); scanf(“%d”, &year); point = &year; printf(“\nThe year is %d.”, year); printf(“\nThe year’s address is %p.”, &year); printf(“\nThe pointer’s address is %p.”, &point); printf(“\nThe pointer’s value is %p, which is the same as the year’s address.”, point); } |
Notice this line: point = &year; We are setting the pointer to equal the address where the variable ‘year’ is stored. Later in the program, we use the variable ‘point’ to show the pointer’s address: printf(“\nThe pointer’s address is %p.”, &point);
Type this source code in your editor and save it as point.c then compile it, link it, and run it.
Incrementing Pointers
We can increment pointers by using ++point. However, the computer will NOT simply add 1 to the pointer’s address.
When you increment a pointer, the computer will jump to the next block of memory. If you are using an int variable, which takes up 2 bytes of memory, then the computer will increment by2, to reach the next block.
If you are using a long variable, which takes up 4 bytes of memory, then the computer will increment by 4, to reach the next block.
Thus, when you increment a pointer, you must keep in mind what type of variable you are using. For example, let’s increment an int variable:
#include<stdio.h> void main( ) { int year = 1776; int *point = NULL; point = &year; printf(“\nThe year is %d.”, year); printf(“\nThe address of year is %p.”, &year); printf(“\nThe pointer is the same as the address: %p.”, point); ++point; printf(“\nAfter incrementing the pointer, year’s address is %p.”, point); ++point; printf(“\nIncrement again and year’s address is %p.”, point); } |
Type this source code in your editor and save it as incint.c then compile it, link it, and run it.
Now let’s try it with a long variable:
#include<stdio.h> void main( ) { long salary = 75000; long *point = NULL; point = &salary; printf(“\nJesse’s salary is %ld.”, salary); printf(“\nThe address of salary is %p.”, &salary); printf(“\nThe pointer is the same as the address: %p.”, point); ++point; printf(“\nAfter incrementing the pointer, salary’s address is %p.”, point); ++point; printf(“\nIncrement again and salary’s address is %p.”, point); } |
Type this source code in your editor and save it as inclong.c then compile it, link it, and run it.
Using a Pointer to Change a Variable’s Value
We can use pointers in many ways. For example, we can use a pointer to change the value of a variable. Let’s learn more about pointers.
When we use *point, we are dereferencing the pointer. This means that we are giving the value of the variable, instead of the value of the pointer.
To change the value of a variable, we must do four things:
- declare a variable with a value
- declare a pointer with the value NULL
- make the pointer equal the address of the variable
- dereference the pointer and make it equal another number
Put these four things together, and you can change a variable’s value like this:
int kids = 4;
int *point = NULL;
point = &kids;
*point = 6;
This changes the value of kids from 4 to 6. Let’s write a program that uses this:
#include<stdio.h> void main( ) { int kids = 4; int *point = NULL; printf(“\nThe Smiths have %d children.”, kids); point = &kids; *point = 6; printf(“\nThe Larks have %d children.”, kids); } |
Type this source code in your editor and save it as chvar.c then compile it, link it, and run it. Your result will be:
The Smiths have 4 children.
The Larks have 6 children.