Unsafe conversion

Is the following conversion safe?

int b[10][10];
char *x;
int a[]={0,1,2,3,4,5,6,7,8,9};

for(int i=0;i<10;i++)
  for(int j=0;j<10;j++)
    b[i][j]=a[i];

for(x=(char *)&b[0];x<=(char *)&b[9][9];x+=sizeof(a+1)) // Problem lies here!
    printf("%d\n",*x);

I don’t think the above conversion in a loop is forsafe (I think it is platform dependent). Please correct me if I am wrong. I am surprised because the code compiles without any warning even when compiling using options -Wall -pedanticin gcc.

+5
source share
2 answers

: 2D int char. undefined, " [/] char" .

. , - char. char undefined, char. , unsigned char, , . , char "".

, sizeof(a + 1), , . a + 1 - int *. x, . ?

... , - . GCC ( , ), char ( ), . , , , - , / .

+7

char * C. .

for(x=(char *)&b[0]; x <= (char *)&b[9][9]; x += sizeof(a+1))  

x = (char*)&b[0]; char . x <= (char *)&b[9][9] , x .

x += sizeof(a+1) iffy. 32- sizeof (int *) , sizeof (int), , , , .

, x += sizeof(a[0]) x += sizeof(b[0]), , , .

+1

All Articles