Archived

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

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

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

Recommended Posts

Again Pointers & arrays and again me 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]'' )
compiler is trying to tell you what i explained constituent parts
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...

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);
*(PtrToInt+2).....

//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;
}

well as you see if you can tell me how * and [] works on multiDimensional arrays.And What is the matter on the code.. i will be happy)

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
15
5. 5

• 10
• 9
• 13
• 9
• 33
• Forum Statistics

• Total Topics
632592
• Total Posts
3007289

×