Sign in to follow this  

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

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", "", "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", "", "Hello" }: char *m[] = { "Game Programming", "", "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
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", "", "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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this