Introduction To Pointers
Introduction To Pointers
Address in C++
Whenever a variable is defined in C language a memory location is
assigned for it in which its value will be stored. We can easily check this
memory address using the & symbol.
If var is the name of the variable, then &var will give it's address. Let's
write a small program to see memory address of any variable that we define
in our program.
#include<stdio.h>
void main()
{ int var = 7;
Cout<<"Value of the variable var is:"<< var;
Cout<<"Memory address of the variable var is:"<< &var;
getch();
}
Output
Value of the variable var is: 7
Memory address of the variable var is: bcc7a00
You must have also seen in the function cin>>, we mention &var to
take user input for any variable var.
Cin>>&var;
This is used to store the user inputted value to the address of the
variable var.
Concept of Pointers
Whenever a variable is declared in a program system allocates a
location i.e an address to that variable in the memory to hold the assigned
value. This location has its own address number which we just saw above.
Let us assume that system has allocated memory location 80F for a
variable a. int a = 10;
#include <stdio.h>
void main()
{ int *ptr = NULL;
}
void main()
/*
'&' returns the address of the variable 'i'
which is stored in the pointer variable 'a'
*/
a = &i;
/*
below, address of variable 'i', which is stored
*/
Cout<<"Address of variable i is"<< a;
/*
below, '*a' is read as 'value at a' which is 10
*/
Cout<<"Value at the address, which is stored by pointer variable a is:"
<<a;
getch();