C Pointers

The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.

Consider the following example to define a pointer which stores the address of an integer.

  1. int n = 10;   
  2. int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.   

Declaring a pointer

The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.

  1. int *a;//pointer to int  
  2. char *c;//pointer to char  

Pointer Example

An example of using pointers to print the address and value is given below.

pointer example

As you can see in the above figure, pointer variable stores the address of number variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.

By the help of * (indirection operator), we can print the value of pointer variable p.

Let's see the pointer example as explained for the above figure.

  1. #include<stdio.h>  
  2. int main(){  
  3. int number=50;    
  4. int *p;      
  5. p=&number;//stores the address of number variable    
  6. printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number.     
  7. printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.    
  8. return 0;  
  9. }    

Output

Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50