• Advertisement
Sign in to follow this  

Whats the difference between pointer char arrays and bidimensional char arrays?

This topic is 4905 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

Hello, - char m[] = "Game Programming"; is a matrix of chars. - If we give only the matrix name, it is truly a pointer to the first element of this matrix - char m[][80] = { "Game Programming", "Gamedev.net", "Hello" } is a matrix of char matrices. But.. what about char *m[] ? Why declaring it a pointer, its possible to write something like char m[][80] = { "Game Programming", "Gamedev.net", "Hello" }: char *m[] = { "Game Programming", "Gamedev.net", "Hello" } This is "killing" me.. I can't understand this. I've read Thinking in C++, but it shows a bit about this. Thanks. Alfred [Edited by - Maquiavel on August 17, 2004 2:34:57 PM]

Share this post


Link to post
Share on other sites
Advertisement
This is because of the way C represents arrays (what you call matrices is called an array in english).

An array can be considered in C as a pointer to an area in memory. This means that if you have an array (for instance of type char[]), then you can use it as if it was a pointer (of type char*).

In your example "Game Programming" is of type char[], so you can use it as if it was of type char*, and write the following:

char * m = "Game Programming";

This also happens when you write:

char * m[] = { "Game Programming", "Gamedev.net", "Hello" };

This defines an array of char* objects. Each of the char[] objects inside the {} on the left is converted to char* and inserted into the array.

Two remarks:
- when using C++, std::string is a superior alternative to c-style strings in most cases
- you cannot convert a char* to a char[]: all arrays are pointers, not all pointers are arrays.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement