Pointers in C Programming – Code Example

In previous posts we concluded the arrays topic. In next 2-3 posts we will discuss about pointers with some code examples. Pointers are used to store memory address of variables or arrays. In following simple program we created a pointer named as countPtr and a variable named as count. Then we assigned the address of the variable count
to the pointer countPtr. Now by using that pointer we can get the memory address of the variable count as well as the value of the variable count.


#include <stdio.h>
int main(void){

   int count = 5;

   int *countPtr;

   countPtr = &count;

   printf("Address stored in countPtr pointer is:\n");
   printf("%p\n", countPtr);

   printf("To get the value of the variable to which the countPtr pointer is pointing, we use dereferencing:\n");
   printf("Value to which countPtr is pointing:\n");
   printf("%d", *countPtr);

   return 0;
}

Output:

Hits: 0

Spread the love
  •  
    2
    Shares
  • 2
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *