C pointer address operator

A. Address Operator in C Programming

In the previous chapter we have learnt about more basic understanding about pointer. In this chapter we will be learning about address operator in C programming.

  1. It is Denoted by ‘&’
  2. When used as a prefix to a variable name ‘&’ operator gives the address of that variable.

Example :

&n  -  Gives address on n

B. How Address Operator works ?

#include<stdio.h>
void main()
{
int n = 10;
printf("\nValue of n is : %d",n);
printf("\nValue of &n is : %u",&n);
}

Output :

Value of  n is : 10
Value of &n is : 1002

C. Visual Understanding :

Consider the following program -

#include<stdio.h>
int main()
{
int i = 5;
int *ptr;
ptr = &i;
printf("\nAddress of i    : %u",&i);
printf("\nValue of ptr is : %u",ptr);
return(0);
}

After declaration memory map will be like this -

int i = 5;
int *ptr;

Before Assigning Address to Variable Pointer
after Assigning the address of variable to pointer , i.e after the execution of this statement -

ptr = &i;

After Assigning Address of Variable to Pointer

D. Invalid Use of Address Operator

  • We cannot use Address operator for Accessing Address of Literals.
  • Only Variables have Address associated with them.
&75
  • (a+b) will evaluate addition of Values present in variables.
  • Output of (a+b) is nothing but Literal , so we cannot use Address operator
&(a+b)
  • Again ‘a’ is Character Literal, so we cannot use Address operator.
&('a')

E. Conclusion :

Thus we have learnt -

  1. About address operator
  2. How Address Operator is used to access the address of Variable
  3. Different illegal or wrong ways of using address operator
  4. Visual Understanding about Address operator

In the next chapter we will be learning Memory Organisation of Pointer Variable.