As we known that the last element value of an array or an two-dimensional array can be obtained by below code:
For an array:
int a[] = {1, 2, 3, 4}; printf("%d", *((int *)(&a + 1) - 1));
For an two dimensional array:
int a[2][2] = {1, 2, 3, 4}; printf("%d", *((int *)(&a + 1) - 1));
Both of those code can output the correct value: 4. But when I changed the code like below:
int foo(int a[][2]) {
return *((int *)(&a + 1) - 1);
}
void main() {
int a[2][2] = {1, 2, 3, 4};
printf("%d", foo(a));
}
Then I cannot get the right value. Is there anything wrong with this code? The main purpose of this code is try to get the last element value without knowing the number of elements. Is this possible? Thanks in advance.