Another way to learn New Technology

What are pointers and its advantages in C Programming? Explaining with example.

0 145

What are pointers and its advantages? Explaining with the Example.

Pointers :

A pointer is a variable that contains an address which is a location of another variable in memory. Since a pointer is a variable, its value is also stored in the memory in another location. Suppose we assign the address of quantity to a variable p. The link between the variables p and quantity can be visualized as shown in the figure.

The address of p is 5048. Since the value of variable p is the address of the variable quantity, we may access the value of a quantity by using the value of p and therefore, we say that the variable p points to the variable quantity. Thus, p gets the name ‘pointer’.

Advantages :

  1. Pointers reduce the length and complexity of a program.
  2. They increase execution speed.
  3. A pointer enables us to access a variable that is defined outside the function.
  4. Pointers are more efficient in handling the data tables.
  5. The use of a pointer array of character strings results in saving of data storage space in memory.

/ * Accessing variables using Pointers*/ :

main ()


int x, y;

int *ptr;

x = 10;

ptr = &x

y = * ptr;

printf ( “value of x is % d \n \n”, x);
printf ( “%d is stored at addr % u \ n” , x, &x);

printf ( “%d is stored at addr % u \n”, * &x, &x);

printf ( “%d is stored at addr % u \n’\ y, &*ptr);

printf ( “%d is stored at addr % u \n”, ptr, &ptr);