Re: Some doubts on variable-length array




"lovecreatesbeauty" <lovecreatesbeauty@xxxxxxxxx> wrote in message
news:1143799245.354575.50340@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I can understand the behaviour of these statements marked `/* A. don't
understand */' in following code, but do not understand the statements
marked `/* B. understand */' .

The behaviour of the following statements of A kind is different to B
kind.

Thank you.

#include <stdio.h>

void foo(int size_x, int size_y, int tab[size_x][size_y])
Here foo is being told that tab is an array with dimensions
[size_x][size_y].
It will assume that this is actually the case, regardless of the true size
of the array passed to it in the calling function.
{
printf("tab[1][1] == %d\n", tab[1][1]);
}

if this were void foo( int **tab),
foo would not know the rank of the array


int main()
{
/* int tab[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; */
int tab[3][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8};

foo(2, 2, tab); /* A. don't understand */
Call function foo, and tell foo that tab is a 2x2 array

printf("%i\n", tab[2][2]); /* B. understand */
printf("%i\n", *(*(tab+2)+2)); /* B. understand */
printf("\n\n");

foo(3, 3, tab); /* A. don't understand */
Call function foo, and tell foo that tab is a 3x3 array

printf("%i\n", tab[3][3]); /* B. understand */
Error - index out of bounds
printf("%i\n", *(*(tab+3)+3)); /* B. understand */
Error - index out of bounds
printf("\n\n");

return 0;
}

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


.



Relevant Pages