Pointers in C examples



Share



Learn pointer declarations, pointer to int, function returning pointer, pointer to function, dangling pointer, void pointer with examples. Subscribe to enable complete access to all , and .

This article will help you to gain required knowledge to successfully complete , which are required to complete and earn course completion certificates for software jobs.

Pointers, Pointer to integer

  • A pointer is a variable whose value is the address of another variable.
  • 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.
    
  • a is an address where an integer can be stored.
  • a is the address of the first byte of the integer(4 bytes).
  • To initialize pointer a we need to assign a to the address of an integer like the follwoing.
    int b; \\b is an integer.
    a = &b;
    
  • An integer pointer can be initialized only with an address of another 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
    $
    
    In the above program we have passed a pointer to integer as argument for function PtrToInt. Inside function PtrToInt we increment the value at address a. As we passed the address of integer i while calling the function PtrToInt, the change we make inside the function PtrToInt at address a changes the content of i in main function. So value of i when printed post the PtrToInt function call, it prints the value as 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 a 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
    $
    
    In the above program we passed a pointer to an integer to function FuncRetPtr. Inside function FuncRetPtr we increment the content at address a by 1 and then print the address a. As the same address a is returned from the function FuncRetPtr, pointer variable p which captures the output of function FuncRetPtr also prints the same address a. So p is same as address of integer i. So *p prints the value of i incremented by 1.

    A pointer to a function

    To Read Full Article

    You need to subscribe & logged in

    Subscribe

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

     Articles

     C Programming

     Linux Software

     Embedded Systems

     Python Programming

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs

     Online certificate courses

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software