2

How can I get the address of a multidimensional static array? For example, this is my array

char array[2][10] = {"word1", "word2"};

Is it possible to get the address to make me reference this array using a pointer like this?

char** pointer;

I tried &array or directly pointer = (char**)array; but it crashes at startup.

5
  • 1
    char** pointer = array is sufficient. If it crashes, you have bug somewhere else. You don't even need a cast. Commented May 31, 2011 at 21:22
  • post your code. pointer=(char**)array; should have worked, if it's crashing - there's some other problem. Commented May 31, 2011 at 21:23
  • Uhm it's crashing because I'm referencing 2nd char* (word2) with "pointer[0]"... Instead using just "pointer" all chars until '\0' is printed thus I see "word1". But why is illegal? char** pointer should be an array of char*, so this expression "pointer[0]" shouldn't be illegal... Commented May 31, 2011 at 21:33
  • read section 6 of the c-faq: arrays are not pointers and pointers are not arrays. Commented May 31, 2011 at 21:36
  • Thanks I knew the difference... Unluckily I left C for too much time to Java, and these are the things you've not to worry about using Java. You think only "every reference is a pointer" :D Commented May 31, 2011 at 22:26

2 Answers 2

5

char **pointer means that a pointer is pointing to a pointer. So *pointer is expected to be a pointer (e.g. 4-byte value which can be interpreted as an address).

This is not the case with your array: it is a contiguous area in memory (20 bytes).

So when you try to convert the array to char ** your application crashes. It's not possible to do this conversion, char ** must point at a pointer.

Sign up to request clarification or add additional context in comments.

3 Comments

And credit to "Expert C Programming" by Peter Van Der Linden who explained so many things I thought I already knew.
Right... "array" can be seen as a simple char* pointer treated as a "bounded" matrix by the compiler. Thus I will get in error assigning this array to an array of pointers. I should initialize every pointer by my self, doing like this? ` char arr[2][20] = {"word1","word2"}; char **pointer = new char*[2]; pointer[0] = arr[0]; pointer[1] = arr[1];`
@Exor: Yes, that would work. But it has to be done "manually", there is no automatic conversion, because the types are incompatible.
2

"array" is the address of the array in memory but it is not a char**. While you can cast it, the application will crash if you try

printf("%s", pointer[1]);

because in your case is probably the same as

printf("%s", (char *)(0x00000031));

since pointer[1] means "the second 4 byte pointer (assuming x86) starting from 'array'". pointer[0] MAY not crash but won't show "word1" either for the same reason.

You probably want (this is hard to remeber so i had to check online, hope it is correct):

char (*pointer)[10] = array;

Which is a pointer to an array of 10 chars. And if you use pointer[1] it now means "the second 10 chars block starting from 'array'".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.