When I try a test program with just these two lines
char array[256];
char** arrayPointer=&array;
I get the error
cannot convert from char*[256] to char**.
Yet if I do this:
char array[256];
char* temp=array;
char** arrayPointer=&temp;
I get no such complaint.
I figured that it was eclipse acting buggy (which my eclipase is acting funny right now) but when I tried to do a cast of the &array to char** for the function I ended up with unusual behavior and my debugger implying that the array isn’t being modified as it should.
PS. all of this was written by hand, forgive typos.
In C++, arrays and pointers are not the same thing. Arrays in many cases can implicitly be converted to a pointer, but array types and pointer types are different.
In your case, the variable
has type
char[256]. If you take its address by writing&array, you get a pointer to an array of 256chars, which has typechar (*)[256]. This is not the same achar**. This is actually a good thing. If you could do the conversion, what would happen if you did this?In this case, the third line would “reassign”
arrayto point to a new array of 256 elements. However,arrayis not a pointer! This operation is meaningless.The reason you got a weird debugger error when writing
is that the cast you’ve put in results in undefined behavior. You’re pretending that a pointer to an array of 256 actual
charobjects is really a pointer to a pointer to achar. This is a meaningless cast, so all bets are off when you do it.On the other hand, if you explicitly introduce a
char*variable like this:Then everything is fine. In the second line, you create a pointer (actual type
char*) that points to the first element ofarray. In the third line, you create a pointer to that new pointer. If you then writeThen nothing bad happens; you’ve just changed where
ptrwas pointing, and didn’t destroyarray.Hope this helps!