Hello, this code fails:
> #include <stdio.h>
> #include <stdlib.h>
> #define M 3
> #define N 3
>
> int
> main ()
> {
> unsigned int contador_x, contador_y, x = 0;
> unsigned int map[M][N] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
> unsigned int (*m2)[M][N] = ↦
>
> /* unsigned int (*m)[][] = ↦*/
> /* void *m = ↦*/
> /* m = malloc(1); */
> /* printf ("m=%p SIZEOF (*m) = %u M[1][0]=%u\n", m, sizeof (m),
> (*m)[1][0]);*/
> /* (*((unsigned int (*)[3][3]) m + 0) + 0) = 3;*/
> /* {
> unsigned int (*a)[M][N] = ↦*/
>
> printf ("INFO: m2=%p SIZEOF (*m2) = %u M[1][1]=%u \n", m2, sizeof (m2),
> (*m2)[1][1]);
>
> printf("Setting...\n");
> for (contador_x = 0; contador_x < M; contador_x++)
> {
> for (contador_y = 0; contador_y < N; contador_y++)
> {
> *(m2)[contador_x][contador_y] = x++;
> printf ("MAP[%u][%u]=%u\n", contador_x, contador_y, x);
> }
> }
>
> printf("Printing ...\n");
>
> for (contador_x = 0; contador_x < M; contador_x++)
> {
> for (contador_y = 0; contador_y < N; contador_y++)
> {
> printf ("[%u][%u]=%u\n", contador_x, contador_y,
> *(m2)[contador_x][contador_y]);
> }
> }
> /* }*/
> /* free (m);*/
> }
>
OUT:
You can see [5][2], imposible with limits in for. Can be a problem with pointer
to multidimensional array?
If you erase line *(m2)[contador_x][contador_y]=x++; , for() continues.
>
> cc test2.c -Wall -L. -s -o test2
>
> ./test2
> INFO: m2=0xbfcd8270 SIZEOF (*m2) = 4 M[1][1]=5
> Setting...
> MAP[0][0]=1
> MAP[0][1]=2
> MAP[0][2]=3
> MAP[1][0]=4
> MAP[1][1]=5
> MAP[5][2]=6
> Printing ...
> [0][0]=0
> [0][1]=1
> [0][2]=2
> [1][0]=3
> [1][1]=4
> [1][2]=1
> [2][0]=3217916696
> [2][1]=3217916740
> [2][2]=10358772
>
gcc-3.4.3-9.EL4
Thanks, and if this is not a bug sorry.