C 5.00 and 5.10 Give Incorrect sizeof() on Pointer to Array

ID Number: Q30481

5.00 5.10 | 5.10

MS-DOS | OS/2

buglist5.00 buglist5.10 fixlist6.00

Summary:

The C version 5.00 and 5.10 compilers do not always calculate the

correct size of an item with sizeof() operator when referring to an

item through indirection.

Microsoft has confirmed this to be a problem in C version 5.10. This

problem was corrected in C version 6.00.

More Information:

The example program below uses the sizeof() operator to determine how

many elements are in a two-dimensional array. The results vary

depending on whether sizeof() is used with array notation or with

indirection. In one case the one case, sizeof(x[0]) is 20, but in

another, the sizeof(*x) is 200.

You can work around the problem by using sizeof(*x+0) to determine the

sizeof() of the second array dimension using indirection.

Sample code

-----------

char x[10][20];

main()

{

printf("sizeof x[0] is %d\n", sizeof(x[0]));

printf("sizeof *x is %d\n", sizeof(*x));

printf("sizeof x[0][0] is %d\n", sizeof(x[0][0]));

printf("sizeof **x is %d\n", sizeof(**x));

}