You are on page 1of 8

The working style of Pointer

When ever we talk about reliability in language for data transaction, the only one word
comes in our mind and that is “Pointer”. Yes, friends today I am going to talk about the
pointer and its behavior. For, more advance concepts I will use C++ as my tool to explain
pointer. I believe you all have some sort of knowledge about C++.

We have different kinds of data types, those are “int”,”float”,”char”,”double”,”long”, and


many more. Let’s take integer pointer.

1) Discussing pointer holding single variable value:

#include <iostream>
using namespace std;

//--------------------------------------------------------------------------------------
// This Program is written by Krunal Soni @ San Jose State University
//--------------------------------------------------------------------------------------
// Purpose: The Purpose of This Program is give idea About Pointer and their Memory Map
// Convention: The Hungerion Standard is used in This Program
// CopyRight: All Copy Rights are reserved By Krunal
//--------------------------------------------------------------------------------------

int main(int argc, char *argv[])


{

int iVar=5;
int *iPtr;
iPtr=&iVar; // you can also intialized iPtr with declaraion For Example, int *iPtr=&iVar;
int **iAddressOfPointer=&iPtr; // this is how double pointer works
int ***iAdressOFAOPointer=&iAddressOfPointer; // You can create as many degree of pointers as
you want
//This Section A, it will show the result of all assigned statements
cout<<"\nThe Value of iVar is: "<<iVar<<endl;
cout<<"\nThe Address of iVar is: "<<iPtr<<endl;
cout<<"\nThe Value pointed by iPtr is: "<<*iPtr<<endl;
cout<<"\nThe Address of iPtr is: "<<iAddressOfPointer<<endl;
cout<<"\nThe Address of iAddressOfPointer is: "<<iAdressOFAOPointer<<endl;
//This the End of section A
cout<<"\n"<<endl;
//This Section B, it will show the resulted Memory map in to the Kernel
cout<<"-------------------------------------------------------------"<<endl;
cout<<"Memory Map of Pointers"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"\nThe iVar value 5 is stored in to the Location A: 0x"<<hex<<iPtr<<endl;
cout<<"\nThe Location A is stored in to the Location B: 0x"<<hex<<iAddressOfPointer<<endl;
cout<<"\nThe Location C is stored in to the Location C: 0x"<<hex<<iAdressOFAOPointer<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"End of Memory Map "<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"\n"<<endl;
//This is the end of Section B
return 0;
}
As shows in the program we have one variable holding the value 5, now where
this 5 value will be stored? Interesting right? This value will be stored in the logical
memory created by compiler (I am using VC++ compiler). The value will be stored in to
the logical address space. If we want to know the exact address where this value will be
stored then we need to create single degree pointer pointing this variable. When we create
this single degree pointer at that time, the address of the variable, i.e. in our case iVar,
will stored in to the single degree pointer. Now, if we want to know the address of single
degree pointer then we need to create double degree pointer and it will does same above
thing. Same way we can go up to any degree of pointer to track the address.

2) Discussing pointer holding single string:

#include <iostream>
using namespace std;

//--------------------------------------------------------------------------------------
// This Program is written by Krunal Soni @ San Jose State University
//--------------------------------------------------------------------------------------
// Purpose: The Purpose of This Program is give idea About Pointers and their Memory Map
// Convention: The Hungerion Standard is used in This Program
// CopyRight: All Copy Rights are reserved By Krunal
//--------------------------------------------------------------------------------------

int main(int argc, char *argv[])


{

char* iString="I am Krunal from California";


char **iAddressOfiStringVariable=&iString; // this is how double pointer works
char ***iAdressOFAOiStringVariable=&iAddressOfiStringVariable; // You can create as many degree
of pointers as you want

//This Section A, it will show the result of all assigned statements


cout<<"\nThe Value of iString is: "<<iString<<endl;
cout<<"\nThe Address of iString is: "<<iAddressOfiStringVariable<<endl;
cout<<"\nThe Address of iAddressOfiStringVariable is: "<<iAdressOFAOiStringVariable<<endl;
//This the End of section A

cout<<"\n"<<endl;

//This Section B, it will show the resulted Memory map in to the Kernel
cout<<"-------------------------------------------------------------"<<endl;
cout<<"Memory Map of Pointers"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"\nThe the address of iString is stored in to the Location A:
0x"<<hex<<iAddressOfiStringVariable<<endl;
cout<<"\nThe Location A is stored in to the Location B:
0x"<<hex<<iAdressOFAOiStringVariable<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"End of Memory Map "<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"\n"<<endl;
//This is the end of Section B

return 0;
}

As shown in the above program, char pointer is holding the string character ‘I’.
When we try to print the address pointed by this character pointer it will print whole
character string till the EOC(end of characters). Also, I have shown second degree
pointer and third degree pointer, to show where these pointers’ location stored in to the
logical memory.

3) Discussing pointer holding the array:

#include <iostream>
using namespace std;

//--------------------------------------------------------------------------------------
// This Program is written by Krunal Soni @ San Jose State University
//--------------------------------------------------------------------------------------
// Purpose: The Purpose of This Program is give idea About Pointers and their Memory Map
// Convention: The Hungerion Standard is used in This Program
// CopyRight: All Copy Rights are reserved By Krunal
//--------------------------------------------------------------------------------------

int main(int argc, char *argv[])


{
char cArrayOfAlphaBetics[5]={'A','B','C','D'};
char* iArrayPtr=cArrayOfAlphaBetics;
char **iAddressOfiArrayPtr=&iArrayPtr; // this is how double pointer works
char ***iAdressOFAOiArrayPtr=&iAddressOfiArrayPtr; // You can create as many degree of pointers
as you want

//This Section A, it will show the result of all assigned statements


cout<<"The First charcter pointed by pointer "<<*iArrayPtr<<endl;
cout<<"The Second charcter pointed by pointer "<<*(iArrayPtr+1)<<endl;
cout<<"The Third charcter pointed by pointer "<<*(iArrayPtr+2)<<endl;
cout<<"The Fourth charcter pointed by pointer "<<*(iArrayPtr+3)<<endl;
cout<<"The fifth charcter pointed by pointer "<<*(iArrayPtr+4)<<endl;
cout<<"The Sixth charcter pointed by pointer "<<*(iArrayPtr+5)<<endl;
cout<<"\nThe Value of iString is: "<<iArrayPtr<<endl;
cout<<"\nThe Address of iString is: "<<iAddressOfiArrayPtr<<endl;
cout<<"\nThe Address of iAddressOfiStringVariable is: "<<iAdressOFAOiArrayPtr<<endl;
//This the End of section A

cout<<"\n"<<endl;

//This Section B, it will show the resulted Memory map in to the Kernel
cout<<"-------------------------------------------------------------"<<endl;
cout<<"Memory Map of Pointers"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"\nThe the address of iString is stored in to the Location A:
0x"<<hex<<iAddressOfiArrayPtr<<endl;
cout<<"\nThe Location A is stored in to the Location B:
0x"<<hex<<iAdressOFAOiArrayPtr<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"End of Memory Map "<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"\n"<<endl;
//This is the end of Section B

return 0;
}
As show above, the program is pretty much self explanory. We are creating one
pointer which points the array of character. It will hold address of the first element of
array and rest of the address are in the sequence in the array. So it can fetch any value. So
if you want to fetch 4th value of the array then just add 3 in to the startring address of the
array. This concept is shown in the program. Same way we can create the array of
integers, strings, floats, double and Etc.

4) Discussing pointer holding the two degree array:

This is kind of cool feature in C and C++. I guess this the power of pointer. The only
concept I like in the C and C++ is “the Pointer”. It is the only concept , which powered
the software industry and this fenomina is undefeatable. So let us discuss the two degree
pointer. Now things will get complex and messy, but let me tell you, once you clear with
this concept you will rule.

#include <iostream>
using namespace std;

//--------------------------------------------------------------------------------------
// This Program is written by Krunal Soni @ San Jose State University
//--------------------------------------------------------------------------------------
// Purpose: The Purpose of This Program is give idea About Pointers and their Memory Map
// Convention: The Hungerion Standard is used in This Program
// CopyRight: All Copy Rights are reserved By Krunal
//--------------------------------------------------------------------------------------

#ifdef DoubleArr_Testing
int main(int argc, char *argv[])
{
char cArrayOfAlphaBetics[3][3]={{'A','B','C'},{'1','2','3'},{'x','y','z'}};
char *iArrayPtr=&(*((char*)cArrayOfAlphaBetics));
char **iAddressOfiArrayPtr=&iArrayPtr; // this is how double pointer works
char ***iAdressOFAOiArrayPtr=&iAddressOfiArrayPtr; // You can create as many degree of pointers
as you want
//This Section A, it will show the result of all assigned statements
cout<<"The First charcter pointed by pointer "<<*iArrayPtr<<endl;
cout<<"The Second charcter pointed by pointer "<<*(iArrayPtr+1)<<endl;
cout<<"The Third charcter pointed by pointer "<<*(iArrayPtr+2)<<endl;
cout<<"The Fourth charcter pointed by pointer "<<*(iArrayPtr+3)<<endl;
cout<<"The fifth charcter pointed by pointer "<<*(iArrayPtr+4)<<endl;
cout<<"The Sixth charcter pointed by pointer "<<*(iArrayPtr+5)<<endl;
cout<<"\nThe Address of iString is: "<<iAddressOfiArrayPtr<<endl;
cout<<"\nThe Address of iAddressOfiStringVariable is: "<<iAdressOFAOiArrayPtr<<endl;
//This the End of section A

cout<<"\n"<<endl;

//This Section B, it will show the resulted Memory map in to the Kernel
cout<<"-------------------------------------------------------------"<<endl;
cout<<"Memory Map of Pointers"<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"\nThe the address of iString is stored in to the Location A:
0x"<<hex<<iAddressOfiArrayPtr<<endl;
cout<<"\nThe Location A is stored in to the Location B:
0x"<<hex<<iAdressOFAOiArrayPtr<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"End of Memory Map "<<endl;
cout<<"-------------------------------------------------------------"<<endl;
cout<<"\n"<<endl;
//This is the end of Section B

return 0;
}
#endif

As shown in above program, it is same as single array declaration but the


declaration of array is pretty much different.
5) Discussing pointer holding the three degree and Multi Degree Array:

This is similar to two dimensions, the only difference is in the initialization part. For
example:

char cArrayOfAlphaBetics[2][2][2]=
{{{'A','B'},{'1','2'}},{{'C','D'},{'3','4'}}};
char *iArrayPtr=&(*((char*)((char*)cArrayOfAlphaBetics)));

Above is the declaration of the three dimensions array and initialization of the pointer.
Like wise we can go up to any level of dimensions.

6) Types of pointer:

We have two types of pointer the one with data type and the other with void type. For,
example int*, float*, and etc are data type pointer and void* is void pointer. In industry
it’s good to use void pointer but be careful when converting it.

#include <iostream>
using namespace std;

//--------------------------------------------------------------------------------------
// This Program is written by Krunal Soni @ San Jose State University
//--------------------------------------------------------------------------------------
// Purpose: The Purpose of This Program is give idea About Pointers and their Memory Map
// Convention: The Hungerion Standard is used in This Program
// CopyRight: All Copy Rights are reserved By Krunal
//--------------------------------------------------------------------------------------

#ifdef VoidPtr_Testing
int main(int argc, char *argv[])
{
int iVariableForPtr=10;
int *iDataTypePointer;
void *v_To_i_VoidTypePointer;

v_To_i_VoidTypePointer=&iVariableForPtr;

iDataTypePointer=(int *)v_To_i_VoidTypePointer; // this statement will convert void pointer to integer


pointer
cout<<"The address indicated by DataType Pointer is: "<<iDataTypePointer<<endl;
cout<<"The value indicated by DataType Pointer is: "<<*iDataTypePointer<<endl;
v_To_i_VoidTypePointer=iDataTypePointer; //this statement will convert integer pointer to void
pointer
cout<<"The Size of int Pointer is: "<<sizeof (void *)<<endl;
cout<<"The Size of float Pointer is: "<<sizeof (float *)<<endl;
cout<<"The Size of double Pointer is: "<<sizeof (double *)<<endl;
cout<<"The Size of long Pointer is: "<<sizeof (long *)<<endl;
cout<<"The Size of Void Pointer is: "<<sizeof (void *)<<endl;
cout<<"The address indicated by VoidType Pointer is: "<<v_To_i_VoidTypePointer<<endl;
cout<<"The value indicated by VoidType Pointer is: "<<(*(int *)v_To_i_VoidTypePointer)<<endl;
return 0;
}
#endif

You might also like