C/C++ Pointer

Pointer In IN C /C++

In C/C++ Pointers is a variable that stores the address of another variable. In a large system, where multiple components are layered one on top of the other/are interacting very closely, it is possible to have situations where all the layers require access to a lot of shared data. In these situations, passing by pointers (references) saves a LOT of memory and time. C++ is mostly use to produced machine code and CPUs use pointers so pointer is very important in C++. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.
For Example
int *marks;
int mymarks=100;

in above example first variable marks (strat with *) is pointer variable and second one mymarks is simple variable.
marks=&mymarks; // its mesnas we store the value of mymarks which is 100 in marks.
in simple the address of mymarks variable is store in marks as we use & sign symbol ,& symbol is used to get the address of the variable.

For example: (Simple example of display the value by using pointer variable


#include<stdio.h>
#include<conio.h>
void main(void)

 {

int  x=100;  // simple variable x hanve value 100

 int *y;             //pointer variable

y=&x;             // y store the address of x 

printf("%p",y) ; // display the address of x

printf("%d",*y) ; // display the value which is store on the address of x which is 100

getch( ) ;

}


For example: (Passing the value to function using reference (address of variable)


#include<stdio.h>
#include<conio.h>
#include<iostream.h>

void add(int *, int *)// add is a function which 
                      //get the address of two values
void main(void)

 {

int x,y; // declaring simple varaible x and y

scanf("Enter the value of x and y %d %d",&x,&y);// Get the value of x and y

add(&x,&y)// Passing the address of x and y to function add

getch( ) ;

}

void add(int *a, int *b)// This function definition in which its received two address and store in two pointer variable *a and *b

{

int sum;

sum=*a+*b;  // add the value on address a and b

printf("The result is %d",sum);// Display the result

}