Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

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.

pointers as variable | Allinonetuts


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);

}

The post What are pointers and its advantages in C Programming? Explaining with example. appeared first on All In One Tuts.



This post first appeared on All In One Tuts | Tech Tutorials And Videos | Tech, please read the originial post: here

Share the post

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

×

Subscribe to All In One Tuts | Tech Tutorials And Videos | Tech

Get updates delivered right to your inbox!

Thank you for your subscription

×