×
>
<

Aptitude

C Pointer

What is a Pointer

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.

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.

Declaration
int *x; //pointer to int  
char *y; //pointer to char      

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

Example :

#include <stdio.h>  
int main(){  
    int number=50;    
    int *p;      
    p = &number; //storing the address of number variable    
    printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number.     
    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.    
    return 0;  
}     

Output :

Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
Facts About Pointer

Advantages

  • Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions.
  • We can return multiple values from a function using the pointer.
  • It makes you able to access any memory location in the computer's memory.

Uses of Pointer

1) Dynamic memory allocation

In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.

2) Arrays, Functions, and Structures

Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.

Pointer Address
The address of operator '&' returns the address of a variable. But, we need to use %u to display the address of a variable.
  
#include <stdio.h> 
int main(){  
    int number=50;   
    printf("value of number is %d, address of number is %u",number,&number);    
    return 0;  
}    
  

Output :

value of number is 50, address of number is 4287450116   
Types of Pointers
There are majorly four types of pointers, they are :
  • Null Pointer
  • Void Pointer
  • Wild Pointer
  • Dangling Pointer

1.Null Pointer

A pointer refering NULL is known as null pointer.
Syntax :
  
Int *var = NULL;
  
Example :
  
#include <stdio.h> 
int main()
{
    int *var = NULL;
    printf("var = %d ",*var);
}   
  
Segmentation fault  

2.Void Pointer

When a pointer is declared with a void keyword, then it is called a void pointer. We need to type cast it first if we want to print it.
Syntax :
  
void *var;
  
Example :
  
#include <stdio.h> 
int main()
{
    int a=2;
    void *ptr;
    ptr = &a;
    printf("After Typecasting, a = %d", *(int *)ptr);
    return 0;
}   
  

Output :

After Typecasting, a = 2  

3.Wild Pointer

A wild pointer is a pointer which is only declared but not assigned to any address of variable. Which may cause segmentation errors..
Syntax :
  
int *ptr;
  
Example :
  
#include <stdio.h> 
int main()
{
    int *ptr;
    printf(“ptr=%d�,*ptr);
    return 0;
}   
  

Output :

Segmentation fault

4.Dangling Pointer

Suppose there is a pointer p pointing at a variable at memory 100005. If we deallocate this memory, then this p is called a dangling pointer.
We can deallocate a memory using a free() function in C.
Syntax :
  
int *ptr;
  
Example :
  
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *ptr=(int *)malloc(sizeof(int));
    int a=5;
    ptr = &a;
    free(ptr); //now this ptr is known as dangling pointer.
    printf("After deallocating its memory *ptr=%d",*ptr);
    return 0;
}   
  

Output :

free(): invalid pointer
Aborted