arrays of arrays and pointers ?

for these declarations :
 
char   t[3][3], *p = (char *)t;

i know that it will make the pointer p have the address of the element t[0][0]
but what is the exact meaning of making such conversion : (char*)t ?
Last edited on
It is an unconditional, I-know-what-i'm-doing kind of cast.

To help the explanation, I'm going to change the subscripts some:

char t[4][3], *p = (char*)t;

By itself, t degenerates into a 'pointer to an array of 3 chars' (char[3]).
The number of arrays of three chars doesn't matter (even though there are four).
So t is the address of (the first char) of (the first array) of (the array of arrays).

The cast, (char*), tells the compiler to forget all that information and instead pretend that address is to a single character (or an array of them -- again, it doesn't matter how many there are).


Just so you know, that line of code bothers people on several levels, but mainly because it mixes pointer type declarations with non-pointer type declarations, which is easily misread.

A good rule of thumb is to keep declarations of different types to one type per line:
1
2
int x, y, z;
float q, p;

Additionally, keep each pointer declaration to it's own line:
1
2
char * s;
char * p;

Rewriting the above, you get:
1
2
char   t[3][3];
char * p = &(t[0][0]);

Hope this helps.
Last edited on
@Duoas : thanks !!. do you want to say for this :
1
2
char t[4][3];
char *p = (char*)t;

that the conversion of t from array of arrays of characters to array of characters chose automatically the first array of characters, and the assignment to the pointer p chose automatically the address of the first character in the first array of characters ?
Last edited on
Topic archived. No new replies allowed.