Pointing Towards "Pointers” (For Begineers)

Leave a Comment
When we declare a variable in C/C++ etc. by statements like int a or int b , it tells the compiler to reserve the space of two bytes in the memory to store the integer type value and identify the storage location in the memory by 'a' or 'b' etc. So every variable has three properties 1. Identifier (Name like a or b) 2. Value(a=10 or b=15 etc) 3. Its address (hexadecimal) in the Temporary Memory i.e RAM . Since compiler makes our job simpler by giving an opportunity to know only the variable’s name and its value then in normal situation we have no need to worry about the location or address of the variable. But in advanced programming and certain types of problems we have to handle and use the address of variables. So the concept of Pointers solves this problem.
If we try to define the Pointer in terms of C/C++ we can say that "Pointers are those variables which holds the address of another variable" . In C/C++ we see the Address or Memory Location Number of any variable by using "&" i.e. if we declare a variable 'a' eg : int a; we can see its address by pritnf statement as follows printf("\n %d",&a); now to handle the address of variables we have the facility to declare the Pointer variable like this int *p; //This statement tells the compilers to reserve the space in memory to store the address of integer type variable. To assign the address of any integer type variable to 'p' we have to write the statement like p=&a;
Following program will clear these things.

#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=8;
/* Declaring int type variable ‘a’ */
int *q=&a, *r=&b; /*Declaring a pointer variable q to hold the address of int type variable ‘a’*/
int c=0;
printf("\n Value of variable a=%d",a);
/*Printing the value stored in variable a */
printf("\n Address of variable a=%d", &a); /*Printing the address in memory of variable a , check the ‘&’ before variable name*/
printf("\n Address of variable a stored in pointer variable q=%d", q); /*Printing the address of variable 'a' stored in pointer variable ’q’ */
printf("\n Value Stored at Address 'p'=%d", *q); / *Printing the value stored at the address stored in pointer variable */
c=*q+*r; / *Adding values which are stored at the addresses (memory locations) stored in pointer variable ‘q’ and ‘r’ */
printf("\n Result *a+*b = %d", c); /* Printing the value of ‘c’ */
printf("\n Address of c=%d", &c); /* Printing the address(Memory Location Address ) of variable ‘c’ */
getche();
}

SAMPLE RUN SCREENSHOT:
Pointing Towards "Pointers” (For Begineers)
 

0 comments: