So far thought it is, but after I learned that the compiler may pad data to align it for architecture requirements for example I'm in doubt. So I wonder if a char[4][3]
has the same memory layout as char[12]
. Can the compiler put padding after the char[3]
part to make it aligned so the whole array takes actually 16 bytes?
The background story that a function of a library takes a bunch of fixed length strings in a char*
parameter so it expects a continuous buffer without paddig, and the string length can be odd. So I thought I declare a char[N_STRINGS][STRING_LENGTH]
array, then conveniently populate it and pass it to the function by casting it to char*
. So far it seems to work. But I'm not sure if this solution is portable.
char [4][3]
norchar [12]
may contain padding, andsizeof
will be12 * sizeof(char)
for both. – Markettamarkeychar [][]
tochar *
? – Lodhiasizeof
will be12
. – Procyon