Power Of Pointer Part 1

  • Uploaded by: krunal
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Power Of Pointer Part 1 as PDF for free.

More details

  • Words: 1,645
  • Pages: 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 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: "<
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 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: "<
cout<<"\nThe Address of iAddressOfiStringVariable is: "<
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 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: "<
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 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: "<
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 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: "<
return 0; } #endif

Related Documents

Power Quality, Part 1
April 2020 19
Map Of Pointer Asia
June 2020 11
Function Pointer
July 2020 14
Funtion Pointer
April 2020 17
Power Of Display 1
November 2019 5

More Documents from ""

May 2020 30
Slam Book
May 2020 27
May 2020 23
A
May 2020 14
May 2020 27