The two fundamental operators used with Pointer are
- The address of operator &
- The indirection operator or value at address operator *
The address operator returns the address of a variable. The indirection operator returns the value of the variable that the pointer is pointing to.
The ‘&’ operator (Address of Operator)
When a variable x is the declared in a program, a storage location of main memory is made available by the complier as shown below: –
- Thus, x is the name associated by the compiler to a location in the memory of the computer.
- Let us assume that, at the time of execution, the physical address of this memory location (called x) is 2002
- Now, a point worth nothing is that this memory location is viewed by the programmer as variable x and by the operating system as an address 2002.
- The address of variable x can be obtained by ‘&’, an address of operator.
- This operator when applied to a variable gives the physical memory address of the variable. Thus &x will provide the address 2002.
Consider the following segment: –
:
x = 10;
printf("\n value of x = %d", x);
printf("\n Address of x =%d", &s);
:
Once this program segment is obeyed, the output would be: –
Free Hand-Written Notes
Value of x = 10
Address of x = 2002 (assumed value)
It may be noted here that the value of address of x is assumed and the actual value is machine and execution time dependent.
The ‘*’ operator (Value at address operator)
- The ‘*’ is an indirection operator or value at address operator.
- In simple words, we can say that if address of a variable is known then the ‘*’ operator provides a way of accessing the contents of the variable.
Consider the following segment: –
:
x = 10;
printf("\n Value of x = %d", x);
printf("\n Address of x = %d", &x);
printf("\n Value of Address %d=%d", &x, *(&x));
:
Once the segment is run, the output would be: –
Value of x = 10
Address of x = 2002
Value of Address 2002 = 10