C program to demonstrate the use of pointers

Basic of pointers

Being a high level programming language, C is extremely powerful at low level programming.Pointer is one of its tool that provide such low level memory handling. Data in memory is organized as a sequence of bytes. Where each byte is accessed through its unique address. These addresses ranges from zero (0) to some positive integer. Pointers in C programming provides an efficient way to handle the low level memory activities.

Reading memory address of any variable

In C programming the unary & (Address of) operator is used to get memory address of any variable. Address of operator when prefixed with any variable returns the actual memory address of that variable.

C program to create, initialize, assign and access a pointer variable.
 
#include <stdio.h>
#include<conio.h>
int main()
{
  clrscr();
  int num;    /*declaration of integer variable*/
  int *pNum;  /*declaration of integer pointer*/

  pNum=& num; /*assigning address of num*/
  num=100;    /*assigning 100 to variable num*/

  //access value and address using variable num
  printf("Using variable num:\n");
  printf("value of num: %d\naddress of num: %u\n",num,&num);
  //access value and address using pointer variable num
  printf("Using pointer variable:\n");
  printf("value of num: %d\naddress of num: %u\n",*pNum,pNum);

 return 0;
 getch();
} 

 Run:

 

 


No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...