Programming in C: Unit III (b): Pointers

Declaring Pointer Variables

with Example C Programs

A pointer provides access to a variable by using the address of that variable. A pointer variable is therefore a variable that stores the address of another variable.

DECLARING POINTER VARIABLES

A pointer provides access to a variable by using the address of that variable. A pointer variable is therefore a variable that stores the address of another variable. The general syntax of declaring pointer variables can be given as below:

Programming Tip: The data type of the pointer variable and the variable to which it points must be the same.

data_type *ptr_name;

Here, data_type is the data type of the value that the pointer will point to. For example,

int *pnum;

char *pch;

float *pfnum;

In each of the above statements, a pointer variable is declared to point to a variable of the specified data type. Although all these pointers, pnum, pch, and pfnum point to different data types, they will occupy the same amount of space in memory. But how much space they occupy will depend on the platform where the code is going to run. To verify this, execute the following code and observe the result.

#include <stdio.h>

main ()

{

int *pnum;

char *pch;

float *pfnum;

double *pdnum;

long *plnum;

printf("\n Size of integer pointer =%d", sizeof (pnum));

printf("\n Size of character pointer = %d", sizeof (pch));

printf("\n Size of float pointer = %d", sizeof (pfnum));

printf("\n Size of double pointer = %d", sizeof (pdnum)) ;

printf("\n Size of long pointer = %d", sizeof (plnum));

}

Output

Size of integer pointer = 2

Size of character pointer = 2

Size of float pointer = 2

Size of double pointer = 2

Size of long pointer = 2

Now let us declare an integer pointer variable and start using it in our program code.

int x = 10;

int *ptr;

ptr = &x;

In the above statement, ptr is the name of pointer variable. The '*' informs the compiler that ptr is a pointer variable and the int specifies that it will store the address of an integer variable.

Programming Tip: A pointer variable can store only the address of a variable.

An integer pointer variable, there- fore, points to an integer variable. In the last statement, ptr is assigned the address of x. The & operator retrieves the Ivalue (ad- dress) of x, and assigns it to the pointer ptr.

Consider the memory cells given in Figure 7.1.

Now, since x is an integer variable, it will be allocated 2 bytes. Assuming that the compiler assigns it memory locations 1003 and 1004, we say the value of x = 10 and the address of x (written as &x) is equal to 1003, i.e., the starting address of x in the memory. When we write, ptr = &x, then ptr = 1003.

Note

In C, pointers are not allowed to store actual memory addresses, but only the addresses of variables of a given type. Therefore writing a statement like int *ptr = 1000; is absolutely illegal in C.

We can dereference a pointer, i.e., refer to the value of the variable to which it points, by using unary *' operator (also known as indirection operator) as *ptr, i.e., *ptr = 10, since 10 is value of x. Therefore, * is equivalent to writing value at address. Look at the code below which shows the use of pointer variable.

Programming Tip: As the address of a memory location is a constant, it cannot be changed in the program code.

#include <stdio.h>

int main()

{

int num, *pnum;

pnum = &num;

printf("\n Enter the number: ");

scanf("%d", &num);

printf("\n The number that was entered is: %d", *pnum);

printf("\n The address of number in memory is: %p", &num);

return 0;

}

Output

Enter the number: 10

The number that was entered is: 10

The address of number in memory is: FFDC

Note

%p control string prints the argument as a memory address in hexadecimal form. %u prints memory address in decimal form.

Can you tell what will be the value of * (&num)? Yes it is equivalent to num. The indirection and the address operators are inverse of each other, so when combined in an expression, they cancel each other.

We can also assign values to variables using pointer variables and modify their values. The code given below shows this.

Programming Tip: A pointer variable cannot be assigned value of the variable to which it points.

#include <stdio.h>

int main()

{

int num, *pnum;

pnum = &num;

*pnum = 10;

printf("\n *pnum = %d", *pnum);

printf("\n num =%d", num);

*pnum = *pnum + 1;

// increments the value of num

printf("\nAfter increment *pnum = %d", *pnum);

printf("\n After increment num = %d", num);

return 0;

}

Output

*pnum 10

num = 10

After increment *pnum = 11

After increment num = 11

Now can you predict the output of the following code?

#include <stdio.h>

int main()

{

int num, *pnum1, *pnum2;

pnum1 = &num;

*pnum1 = 10;

pnum2 = pnum1;

printf("\n Value of num using all three variables (num, *pnum1, *pnum2) = %d %d %d", num, *pnum1, *pnum2);

printf("\n Address of num using all three variables (&num, pnum1, pnum2) = %x %x %x", num, pnum1, pnum2);

return 0;

}

While the first printf statement will print the value of num, the second printf statement will print the address of num. These are just three different ways to refer to the value and address of the same variable.

Note

Any number of pointers can point to the same address.

The address of a variable is the address of the first byte occupied by that variable. Basically, address of the variable is the relative location of the variable with respect to the program's memory space. Although the address of a variable cannot be changed, the variable's address may change during different program runs, i.e., if you try to print the address of num today, it may print 4010. Next time when you run the program, it may print the address as FA12.

Programming Tip:

It is an error to assign address of a variable to another variable of the same type. The variable to which the address is being assigned must be declared as a pointer variable.

One point to remember always is that both the data type of the pointer variable and the variable whose address it will store must be of the same type. Therefore, the following code is not valid.

int x = 10;

float y = 2.0;

int *px;

float *py;

px =&y; //INVALID

py = &x; //INVALID

Also note that it is not necessary that the pointer variable will point to the same variable throughout the program. It can point to any variable as long as the data type of the pointer variable is same as that of the variable it points to. The following code illustrates this concept.

#include <stdio.h>

int main()

{

int a=3, b=5;

int *pnum;

pnum = &a;

printf("\n a = %d", *pnum);

pnum = &b;

printf("\n b = %d", *pnum);

return 0;

}

Output

a = 3

b = 5

Note

Using an un-initialized pointer can cause unpredictable results.

Programming in C: Unit III (b): Pointers : Tag: : with Example C Programs - Declaring Pointer Variables