Professional Documents
Culture Documents
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++.
#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 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.
#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
//--------------------------------------------------------------------------------------
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.
#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
//--------------------------------------------------------------------------------------
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.
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
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;