Pointers in C programming Language.

Variables,Addresses and Pointers:
Pointer: A pointer in C programming is a variable that stores the address of another variable in the memory variable.
It is much important to differentiate between variable, pointer and the address that the pointer holds. This is the source of much of the confusion about pointers. From the given fig.We will illustrate the code snippets.
int n= 3;
int *ptr;
ptr=&n;
In the first statement we have declared a variable “n” of type integer and assigns a value 3 to it.
The second statement int *ptr declares a pointer variable that holds the address of an integer variable.
The Third statement  ptr=&n   assigns the address of the variable “n” to pointer variable ptr.
for our understanding let study from fig.
The variable n is declared as integer and assigned value 3 and whose memory address is 2000.
After that we declared a integer pointer variable int *ptr. The pointer vaiable ptr stores the address of another integer variable.
ptr=&n;
The above statement assigns the address of variable n to pointer variable ptr.
Now the value of ptr is 2000.
Indirection:
The Method of accessing the value at the address held by a pointer variable is known as Indirection. The indirection operator (*) is used to access the value at the address held by pointer variable. The * operator is also called the dereference operator.
Let understand the code snippet given.


#include<stdio.h>
int main()
 {
  int n= 3;
  int *ptr;
  ptr=&n;
  printf(“\nThe value of n using pointer variable is %d”, *ptr);
  return(0);
 }
Output:
The value of n using pointer variable is 3

The output of the given program is 3 . we need to remember that the “ptr” is a pointer variable that hold the address of the variable “n”.
Observe from the given program that we have declared int *ptr pointer variable and assigned the address of variable n to it.
ptr=&n; and finally we accessed the value of n using *ptr variable called "indirection" Operator. In other word *ptr is also known as value at address operator.
The pointer variable *ptr becomes--->*(2000) .we know from our program that the value at address 2000 is 3.
The function printf() displays the final output.
"The value of n using pointer variable is 3."



Previous Topic:-->> Union FAQ in C. || Next topic:-->>How to Use Pointer in C.