# 14.3 – Pointers in action

by on January 16, 2018

### Pointers in action:

So far we know how to get the address of a variable using Address of operator &, how to declare a pointer and how the address of a variable can be assigned to the pointer. Now in this session, we will see how pointers can be used?

We know that, we can access the data/value stored in the memory using the name of variable directly like

```x=45;            /* Assigning */
x=x-20;          /* modifying */
printf("%d",x);  /* printing   */
```

It is also possible to access the data/value stored at a particular address in the memory indirectly using the value at operator *
Say for example, if the address of x is 65522 then we can access the value of x through its address using the value at operator *

Here 65522 is the address of x, which can be written as &x
Value at 65522 is 345, which can be written as value at &x
Value at can be replaced with the operator * hence value at &x can be written as *(&x)

```#include <stdio.h>
int main()
{
short x=345;
printf("x=%d",x);
printf("\nx=%d\n",*(&x));
return 0;
}
```

Output:
x=345
x=345

Another way of indirect accessing is through the pointer. Because the pointer holds the address of another variable, we can access the value at an address through the pointer.

Here p is the pointer to the variable x, which holds the address of x.
Value at 65522 is 345; here 65522 can be replaced with p as the pointer holds the address of x, equivalent statement to value at 65522 is value at p.
As the value at can be replaced with the operator *, equivalent statement to value at p is *p

```#include <stdio.h>
int main()
{
short *p;                 /* declaration of pointer  */
short x=345;              /* declaration of variable */
p=&x;                     /* assigning the address to the pointer */
printf("x=%d",x);         /* direst accessing */
printf("\nx=%d",*(&x));   /* indirect access through the address */
printf("\nx=%d\n",*p);    /* indirect access through the pointer */
return 0;
}
```

Output:
x=345
x=345
x=345

### Summarizing value at operator *

• * is the value at operator used to access the data indirectly through the address
• It is a unary operator
• It is also called indirection operator as it is used to access the data indirectly through the address
• Accessing the data indirectly using indirection operator is called indirect reference or dereferencing

### Possibility of confusion

Here * is being used in two places, that are while declaring a pointer and while accessing the value indirectly. Though appears similarly, functionally both are different.
If we use * while declaring a variable then it is to inform the compiler that it will be used to store the address of another variable

```short *p;
```

Here we are informing the compiler that, p is a pointer variable will be used to store the address of another short type variable.

```float *q;
```

Here we are informing the compiler that, q is a pointer variable will be used to store the address of another float type variable.

If * operator is used in other places than the declaration statements then it is to perform indirect reference or dereference operation that is to access memory allocation through address or pointer

```printf("%d",*(&x));
```

It is to access and print the value at the address of x that is, value at 65522, which is 345

```printf("%d",*p);
```

It is to access and print the value at p, that is value at 65522, which is 345

```*p=400;
```

It is to assign value at p, that is value at 65522, which is the value of x

```*p=*p+100;
```

It is modifying the value at p, that is value at 65522, which is the value of x

```#include <stdio.h>
int main()
{
short *p;              /* declaration of pointer  */
short x;               /* declaration of variable */
p=&x;                  /* assigning the address to the pointer */
*p=400;                /* assigning value to x through dereference */
printf("x=%d",*p);     /* printing the value of x through dereference */
*p=*p+200;             /* incrementing the value of x through dereference */
printf("\nx=%d\n",*p); /* printing the value of x through dereference */
return 0;
}

```

Output:
x=400
x=600

### Dereferencing with scanf()

While reading the date from the keyboard, we specify the address of a variable with scanf() like

```scanf("%d",&x);
```

Here p is the pointer which stores the address of x, hence writing p is same as writing &x

```scanf("%d",p);
```

### Multiple references to a common variable

Address of a variable can be stored in multiple pointers so that, any pointer can access the value of variable by dereferencing.

```short x=345;
```

A short type of variable x is declared and assigned with 345

```short *p,*q;
```

p and q are short type of pointers and capable to store the addresses of short variables

```p=q=&x;
```

In the expression both p and q are assigned with the address of x. The same statement can be written as

```p=&x;
q=p;
```

Here the address of x is assigned to pointer p and the value of p (address of x) is again assigned to q.
Now both the pointers are capable to access the value of x using dereferencing or indirection as value at p (*p) and value at q (*q)

```#include<stdio.h>
int main()
{
int x=345;
int *p,*q;
/* p=q=&x; */
p=&x;
q=p;
printf("Value of x %d",x);
printf("\nValue of x %d",*p);
printf("\nValue of x %d",*q);
return 0;
}
```

Output:
Value of x 345
Value of x 345
Value of x 345

### We can’t get the address of a constant and an expression

```#include<stdio.h>
int main()
{
int a=10,b=12;
int *p,*q;
p=&120;    /* assigning the address of constnt    */
q=&(a+b);  /* assigning the address of expression */
return 0;
}
```

Output:
Error pointer1.c 6: Must take address of a memory location in function main
Error pointer1.c 7: Must take address of a memory location in function main expression

Here we try to access the address of a constant (120) and an expression (a+b) using address of operator & but, it can’t return the address of a variable or an expression, can only return the address of a variable. Hence we can’t create a pointer to the constant or an expression.

What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=20,*p=&x;
x=x+40;
printf("\nx=%d",x);
*p=*p+30;
printf("\nx=%d",x);
return 0;
}
```
x=60
x=90
Here the expression *p=*p+30; increments the value of x indirectly through the pointer.

What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=20,y=40;
int *p=&x,*q=&y,*r;
r=p,p=q,q=r;
printf("%d",*p);
printf("\n%d",*q);
return 0;
}
```
40
20
Here p has the address of x and q has the address of y. The expression r=p, p=q, q=r; swaps the addresses. Now p points y and q points x, *p fetches the value of y and *q fetches the value of x

What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=1,y=2,*p=&x;
y=*p;
*p=0;
*p=*p+10;
y=*p+1;
*p+=1;
(*p)++;
printf("\nx=%d",x);
printf("\ny=%d\n",y);
return 0;
}
```
Output:
x=12
y=11

y=*p;          /* Assigns 1 to y  */
*p=0;          /* Assigns 0 to x */
*p=*p+10;  /* Increments the value of x by 10 that x would be 10 */
y=*p+1;      /* assigns 11 to y */
*p+=1;        /* increments the value of x by 1 that would be 11  */
(*p)++;       /* increments the value of x by 1 that would be 12  */

Previous post:

Next post: