Advertisement Jump to content


This topic is now archived and is closed to further replies.


C++ Gurus help me Pointers&Arrays...

This topic is 5338 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Arrays & Pointers... I don''t know why i am that much fastidious about that issue.But There is always some part that confuses me... Now Listen up Please (and find where i am confused)

#include <iostream>

using namespace std;
int main()
int MyInt = 7;       // nothing confisung a standart int

int* PtrToInt=&MyInt;  //nothing confisung a standart int* takes an andress of int..

int** PtrToPtrToInt=&PtrToInt; //nothing confisung a standart int** takes an adress of in**

//Now Let''s make an array of ints and array of arrays of int(multidimensional)

int SingleArray[5]={311,312,313,314,315};
int DoubleArray[4][2]={11,12,13,14,15,16,17,18}; //no need to use brackets in multi array                              

                                                                                   // initializations...(if you want u can)

//now These are also simple to understand so quickly get over with them

cout<<MyInt<<endl;  //that prints 7

cout<<PtrToInt<<"  " <<&MyInt <<endl;   //these both prints the same adress;

cout<<PtrToPtrToInt<<"  "<<&PtrToInt<<endl;  //These both do so..

//well now Get an * on of them

cout<<*PtrToInt<<"  " <<MyInt<<endl;  //These must print 7;

cout<<*PtrToPtrToInt<<"  "<<PtrToInt<<endl; //These print The Same

//     (These print or These printS (i am foreign which one is true)with s or not?

/*  Now what i think (actually sure ) is * operator takes the value of pointer (no mather what it''s type is int* int** int*** ) and acts like that value as an adress and them returns the value 
of that adress.thats why *PtrToInt returns 7.Because it takes The value of PtrToInt which is
adress of MyInt and than returns the value of that adress).No Matter still but don''t worry there will be one (I am not trying to give you a course of  Pointers :
// Now we will integrate pointes with arrays...

/* As we all know the name of an array is the adress of it''s first element.Actuall This is true but NOT THAT MUCH TRUE..Ýf we are defining a multi dimensional array then name is
not the adress of first element .Ý mean Some Of You are thinking that

int SingleArray[3]={1,2,3};  //SingleArray is the adress of first INT value; //HERE TRUE
int DoubleArray[3][2]={11,12,13,14,15,16}; // Here DoubleArray is not address of first elem.

Actually the name of Array is the adress of the FIRST  "one smaller constituent part" of array.
If your array is  a single dimensional array 10 int length just as 
                  Ýnt SingleArray[10]; well then "one smaller constituent part" is an int..
So the array name SingleArray is the adress of that Smaller constituent part(int)

But if Your Array is multi dimensional aray just as 
                  Int DoubleArray[3][2]; 
That multidimensional array is constituted by 3  single dimensional array so "one smaller 
Constituent part" of that multidimensional array is a Single dimensional array 2 length
And The name of MultiDimensionalArray is the adress of that FÝRST "one smaller constituent part" the adres of first SingleDimensional Array
Thats why     DoubleArray==DoubleArray[0];  //The Same adress
Thats why      (DoubleArray+1)==DoubleArray[1] //The Same Adress
Thats why even *(DoubleArray)==DoubleArray //That is most confisung....
//Can anyone tell me why this is  3=2 ...(impossible...)

But wait a minute if you write the code above you saw that compiler didn''t let you to do that and gave an error such (error C2446: ''=='' : no conversion from ''int *'' to ''int (*)[2]'' )
Here is the confisung part if  you don''t write that on your code and on debug write these
To see their adress'' you''ll see what i am saying is true... how *(DoubleArray) has the same adress With DoubleArray...
Then what is * doing there (an advertisement:)

And one more thing what [] does in multi dimensional arrays because
When we say
         DoubleArray[0]  ;  it returns the the adress of first array(first one smaller constituent)
                                        Which is actually the adress DoubleArray means
   //confusing part  when we say 
        DoubleArray[0][2]; here it returns the value of an int (second integer on array)
Ýt can be taking and adress and returning it''s value because in DoubleArray[0] it takes an adress and returning the same adress .But in DoubleArray[0][2] it takes the same adress 
But second []  is having a  effect of   
    Ýnt* PtrToInt=reinterpret_cast<int*>(DoubleArray);

//well don''t say me why you are curious about these kind of things if i learn i learn completely without any black point.. if i don''t i just throw it away..
Please only good Satisfying answers( not confising ones and i don''t know assembly or any low level language if the matter is releated with these..)

return 0;
and moderators excuse me i posted that again because nobody answered...

Share this post

Link to post
Share on other sites
Your question isn''t clear. I don''t think using the source tags with this example was a good idea - it makes it harder to read what you wrote because the long sentences do not wrap around.

Are you asking why *DoubleArray == DoubleArray?

A pointer stores the address of a place in memory where a value is stored. A pointer pointer stores the address of a place in memory where the value stored is also an address of a place in memory where a value is stored.

It might help to return to the hardcoded example you presented - with the inner brackets made explicit. Think of this as a block of 24 bytes.

int DoubleArray[3][2]={ {11,12}, {13,14}, {15,16} };

DoubleArray is basically a pointer pointer. Dereferencing DoubleArray exposes a pointer to the first element of the outer array - ie {11,12}. The first element of this array has the same address as the outer array because that is where the block of bytes begins.

Share this post

Link to post
Share on other sites
I can''t say that I understood your question exactly but here goes:

It might help you to look at one-dimensional arrays as pointers.Think of that:

int IntArray[5];
int *IntPtr;

you can now index IntPtr just like an array,that is IntPtr[3] for example is legitimate.In other words,a pointer to an integer and a 1D array of integers are pretty much the same thing.

Now let''s take a 2D array;

int DoubleArray[5][2];

DoubleArray is an array of pointers(5 of them).Each of these pointers points to a block of 2 ints.
I think it''s easier to grasp it if you break the 2D array in two parts:

1)Think DoubleArray as a 1D array of 5 pointers.
2)Think DoubleArray[0],DoubleArray[1],DoubleArray[2] as a 1D array of ints.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!