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

Started by
1 comment, last by Maquiavel 19 years, 8 months ago
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]
Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]
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.
Thanks a lot ToohrVyk! I have no doubts anymore.
Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]

This topic is closed to new replies.

Advertisement