Advertisment

Programming with pointers

author-image
CIOL Bureau
Updated On
New Update

Pointers are one of the most important concepts in C. Initially, it may seem

a bit confusing but once you understand their working you will realize that

pointers are a very powerful concept and really not all that difficult to

master. Pointers are what distinguish C from other languages. The requirement

for pointers arises from the need to effectively handle data structures like

arrays, linked lists, trees and graphs. Some of these concepts will be explained

later once we’ve mastered pointers. There are several other advantages in

using pointers:

Advertisment
  1. Pointers increase the execution speed.
  2. Saves data storage space in memory.
  3. Reduce the length and complexity of a program.
  4. Allow passing arguments to a function when the original argument needs to

    be modified (passing by reference).

Memory allocation



The computer’s memory is a sequential collection of storage cells and each
of these storage cells has an address associated with it. Typically, the

addresses are numbered consecutively starting from zero to the last address

which depends on the memory size.



Whenever a variable is declared, the compiler allocates some memory for that
variable. This memory location has a unique address and this location holds the

value of the variable. For example:

Advertisment

 int age=35;

This allocates memory for the variable age and the address of this memory

location is let’s say 2000. So, the value 35 is put in the location 2000.

During the execution of the program, the system always associates the variable

age with the address 2000. As you can see memory addresses are simply numbers

and hence can themselves be assigned to another variable. This variable that

holds the address of another variable is in turn stored in memory. Now, to

access the address of a variable we can make use of the address operator &.

For example:

  p=&age

Advertisment

The & operator returns the address of the variable associated with

it, so in this case p would be assigned the address 2000. Since p is also a

variable, its value too is stored in another memory location, let’s say 5000.

The variable that holds a memory address is called a pointer. In the

above example, the pointer would be p. A pointer is nothing

but a variable that contains an address which is the location of another

variable in memory. Thus the variable p is said to point to the variable age.



Now we have two ways to access the value of a variable, which in this case is
35, either by using the name, age or the address 2000. We’ve already used the

address operator & in the scanf function.

Declaring and initializing pointers



The declaration syntax for pointers is as follows:






 datatype *pointer_name;


The above declaration indicates to the compiler that the variable pointer_name
is a pointer variable because of the asterix(*) before pointer_name and that

pointer_name points to a variable of type datatype. For e.g.






 int *p;


 p=&age




This declares p as a pointer variable that points to an integer datatype. Here,

one thing to be kept in mind is that the datatype int refers to the datatype of

the variable being pointed to by p, which is age, and not the datatype of the

pointer. If we had a float variable, say salary, then this is how we’d have to

access its address:

Advertisment

 float sal, *q;



 q=&sal

We must ensure that pointer variables always point to the corresponding type of

data.

This is how we make a pointer variable point to another variable. This

process is called pointer initialization. Remember that a pointer should not be

used before it is initialized.

Advertisment

So far, we have we’ve seen how to assign the address of a variable to a

pointer. Now to access the value of the variable being pointed to we make use of

another operator, the unary operator asterix(*). Consider the example:



 main()



 {



   int age, x, *p;


   age=35;


   p=&age


   x=*p;


   printf("Address of the
variable age = %u\tAge = %d\n",p,x);






   /*Variations that are possible*/


   printf("%d is stored at
address %u\n",age,&age);



   printf("%d is stored at
address %u\n",*&age,&age);



   printf("%d is stored at
address %u\n",*p,p);



   printf("%d is stored at
address %u\n",x,&*p);



   printf("%d is stored at
address %u\n",p,&p);



   printf("%d is stored at
address %u\n",x,&x);



   *p=25;


   printf(" The new value of
age = %d\n",age);



 }














Output of the above program would be :

Address of the variable age = 2000 (let’s say) Age = 35



35 is stored at the address 2000


35 is stored at the address 2000


35 is stored at the address 2000


35 is stored at the address 2000


2000 is stored at the address 5000(let’s say)


35 is stored at the address 2050(let’s say)


The new value of age = 25





Also age = *(&age) = *p = x


        &age =  &*p 








The value of p is the address of age and the statement *p=25 replaces the old

value of 35 by the new value 25. This is equivalent to saying age=25. Thus we

can change the value of a variable indirectly using a pointer and the

indirection operator.

tech-news