I have this multi-dimensional array:
char marr[][3] = {{"abc"},{"def"}};
Now if we encounter the expression *marr by definition (ISO/IEC 9899:1999) it says (and I quote)
If the operand has type ‘pointer to type’, the result has type ‘type’
and we have in that expression that marr decays to a pointer to his first element which in this case is a pointer to an array so we get back ‘type’ array of size 3 when we we have the expression *marr. So my question is why when we do (*marr) + 1 we add 1 byte only to the address instead of 3 which is the size of the array.
Excuse my ignorance I am not a very bright person I get stuck sometimes on trivial things like this.
Thank you for your time.
It adds one because the type is char (1 byte). Just like:
When you dereference a
char [][]it will be used aschar *in an expression.To add 3, you need to do the arithmetic first and then dereference:
You were doing:
which dereferences first.