Pointers in C examples


Advertisements

Share



Learn pointer declarations, pointer to int, function returning pointer, pointer to function, dangling pointer, void pointer with examples.

Pointer to integer

  • A pointer to integer holds an address of an integer.
  • A pointer to integer is declared using the following statement.
  • int *a; \\a is a pointer to integer.
    

    Let's understand the application of pointer to an integer using an example program below.

  • Illustration example
  • 
    #include "stdio.h"
    void PtrToInt(int *);
    int main(void)
    {
      int i = 10;
      PtrToInt(&i);
      printf("%d\n", i);
    }
    void PtrToInt(int *a)
    {
      *a += 1;
    }
    
    
    Output:
    $ gcc prog.c
    $ ./a.out
    11
    $
    

    A function returning a pointer to integer

  • A function returning a pointer to integer returns the address of an integer. You can get the output of the function using a pointer to an integer.
  • A function returning a pointer to integer is declared in the below statement
  • int *a(); \\a is a function returning a pointer to integer.
    

    Let's understand the application of a function returning a pointer to an integer using an example program below.

  • Illustration example
  • 
    #include "stdio.h"
    int *FuncRetPtr(int *);
    int main(void)
    {
      int i = 10;
      int *p;
      p = FuncRetPtr(&i);
      printf("Address at p:%x\n", p);
      printf("Content at p:%d\n", *p);
    }
    int *FuncRetPtr(int *a)
    {
      *a += 1;
      printf("Address at a:%x\n", a);
      return a;
    }
    
    
    Output:
    $ gcc prog.c
    $ ./a.out
    Address at a:7ebfa210
    Address at p:7ebfa210
    Content at p:11
    $
    

    A pointer to a function

  • A pointer can point to a function i.e. we can store the address of a function using a pointer variable
  • A pointer to a function can be declared as shown in the below statement
  • int (*a)(int); \\a is a pointer to a function that takes an integer argument and returns an integer.
    

    Let's understand the pointer to a function using a real example below. Here f is a pointer to a function funct. The function can take an integer as argument and return an integer.

  • Illustration example
  • 
    #include "stdio.h"
    int funct(int );
    int main(void)
    {
      int b = 10;
      int (*f)(int a);
      f = funct;
      int res;
      res = (*f)(b);
      printf("Result:%d\n", res);
    }
    int funct(int a)
    {
      a += 1;
      return a;
    }
    
    

    Dangling pointer

    Dangling pointer
  • A pointer pointing to a memory location that has been deleted (or freed) is called dangling pointer.
  • Let's look at the following example program to demonstrate the use of dangling pointer. In this program memory is allocated first and then freed using ptr Once ptr is freed it becomes a dangling pointer. We need to initializeptr to NULL to remove this dangling pointer.

    
    #include "stdlib.h"
    int main()
    {
      int *ptr = (int *)malloc(sizeof(int));
    
      // After below free call, ptr becomes a
      // dangling pointer
      free(ptr);
    
      // No more a dangling pointer
      ptr = NULL;
    }
    

    Void pointer

    Void pointer

    Rule of pointer is that a pointer should be assigned an address of the same type as mentioned in pointer declaration. But an exception to this is a pointer to void.

  • A void pointer is declared as shown in below statement.
  • void *ptr;
    
  • A pointer to void is a generic pointer that can point to any data type.
  • We can assign address of any data type to a void pointer and a void pointer can be assigned to a pointer of any data type.
  • A void pointer can't be deferenced simply by using indirection operator.
  • Before dereferencing we should type cast to appropriate pointer data type.
  • 
    #include "stdio.h"
    int main(){
      void *ptr;
      int aa = 2;
      float dec = 1.5;
      float *fptr = &dec;
      ptr = &aa;
      printf("%d\n",*(int *)ptr);
      ptr = fptr;
      printf("%f\n",*(float *)ptr);
      return 0;
    }
    
    $ gcc prog.c
    $ ./a.out
    2
    1.500000
    $
    
    NULL pointer

    A pointer that does not contain a valid memory address or is uninitialized, called NULL pointer. In C word NULL is a constant and has value of 0. The following expressions illustrate the correct and incorrect use of the NULL pointer constant.

          
    int *p = NULL;         // Correct
    char *p = 0;           // Correct
    int a = NULL;          // Incorrect 
    int a = 0;             // a is zero of int type
    if( p == 0 );          // Correct
    if( p == NULL );       // Correct 
    if( p );               // Correct
    if( a == 0 );          // Correct
    char *ptr='\0';        // Correct
    char *ptr=(char *)0;   // Correct
    if( a == NULL );       // Incorrect
    if( NULL );            // Correct 
    if( NULL == 0 );       // Correct
    NULL = 0;              // Incorrect
    
  • Example use case of NULL pointer
  • NULL pointer is used to check the return value from memory allocation using malloc. If the return value is NULL that means memory allocation failed. This is illustrated in the below program.

    
    #include "stdio.h"
    #include "stdlib.h"
    int main(){
      int *ptr;
      ptr = (int*)malloc(sizeof(int));
      if(ptr == NULL)
      {
      	printf("Memory allocation failed\n");
      }else
      {
      	printf("Memory allocation successful\n");
      }
      return 0;
    }
    
    $ gcc prog.c
    $ ./a.out
    Memory allocation successful
    $
    

    Exercises, Solutions

    High
    High
    High
    High
    Medium
    Medium
    High
    High

    Want to contribute a new article? Write and upload your article information .
    Share

     Articles

     C Programming

     Linux Software

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs

    Glass Elite Apple iPhone 11 Pro (Case Friendly)

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software