C++ Pointers

October 09, 2015 Posted by WithU Technologies
To understand Pointers, we have to understand that how various data types and variables are stored within computer memory.

As we know that the basic memory storage for application in computer is RAM (Random Access Memory).

So if we consider this as a part of a RAM and each segment or box holds 1-byte of memory storage.



301


302


303


304


305


306




And each 1 byte of memory has an address to identify and access. And thus we provide certain address to each box.

As we know computer allocates different amount of memory storage to each and every data type.

Such as,

char – 1 byte

int – 4 bytes

float – 4 bytes

double – 8 bytes

long double – 16 bytes
·         So, if we declare a variable x with integer data type, then the computer will allocate 4 bytes of memory from the storage. For instance, 301,302,303 and 304 is allocated.

·         Then if we declare a character variable such as char c, the computer will look for some free space to allocate 1-byte of memory to that character variable and ultimately it allocates 305 to it.

·         So if we initialize any of the above variable like, int x =9; and thus the computer stores the value 9 to the memory allocated for integer datatype.

·         To access a memory address, we use ampersand (&) operator also called the (address of operator), which denotes an address in memory.

#include <iostream>
using namespace std;

int main() {

      int x;

      cout<<&x;  //Outputs 0x24fe1c

}

Here the address for integer x variable is 0x24fe1c.

To know or operate upon these memory addresses, we use pointers. Pointer is a data type that contains an address.

As like other variable, we also need to declare a pointer and also define a data type for them. And to declare a pointer we need to use an asterisk sign (*) before the variable name.

#include <iostream>
using namespace std;

int main() {

      int x;

      int *poin;

      cout<<&x<<endl;  //Outputs 0x24fe1c

      cout<<&poin<<endl; //Outputs pointers location where it stored i.e. 0x24fe10

      poin=&x; 

      cout<<poin<<endl;   //Outputs the location of x

}

·         Here we declare poin as the pointer variable which assigns it to the memory location of x using ampersand operator before it.

Dereference Operator (*)

Dereference operator is used to obtain the value located at the specified address. The asterisk (*) used in declaring a pointer is different from the asterisk used for dereferencing. They are simply two different things shown with the help of one symbol.

#include <iostream>
using namespace std;

int main() {

      int x =25;

      int *poin;

      poin=&x;

      cout<<*poin<<endl;   //Outputs 25

      x=x+6;            

      x=poin+6;

      poin=poin+6;        /* All the three syntax ouputs the same value,

                             as x and poin variable are stored in the same storage

                             thus containing the same value  */         

}

Here dereferencing operator is use before the poin variable to get the value of x.

As the location for both the variable are assigned to be same. Thus their value is also dynamically stored in the same storage.