Convert lvalue-to-rval array to ISO C

C ++ ANSI ISO IEC 14882 2003 Appendix C.1 (p. 668):

Edit: The result of a conditional expression, assignment expression, or comma expression can bean lvalue
Rationale: C ++ is an object-oriented language, focusing more on lvalues. For example, functions may return lvalues.
Impact on the original function: changing the semantics of a well-defined function. Some C expressions that implicitly rely on lvalue-to-rvalue conversions will give different results. For example,

char arr[100]; sizeof(0, arr) 

gives 100 in C ++ and sizeof(char*) in C.
...

I read it only today, and I remembered that after a couple of months my friend suggested a problem that should write a function that would return 0 if it was compiled with C ++ and 1 if it was compiled with C I solved it, taking advantage of the fact that in C the structure was in outer space. Therefore, considering this new information, I decided that it would be another solution to the aforementioned problem that I tried in Microsoft Visual Studio 2008, but regardless of whether it is compiled as C or C ++ code sizeof(0, arr) , always it turns out 4. So 2 questions:

1. What is ISO C? Is this the current standard C? This is the only one (I hear that C is developing rapidly) 2. Is this a Microsoft C ++ bug?

TIA

Edit: Sorry you mixed up with the output and edited it:

+6
c ++ c language-lawyer visual-studio-2008 lvalue-to-rvalue
source share
3 answers

Or simply microsoft C is not ISO C, but some other C standard (if one exists).

Microsoft Visual C still supports C89 [only], while other compilers, such as gcc / clang, support C99 , which is the current standard.

C99 [Section 6.5.17/2 ] says

The left operand of the comma operator is evaluated as a void expression; after its evaluation, a sequence point appears. Then the right operand is evaluated; The result has its type and meaning. 95

Thus, the result of sizeof (0,arr) will be sizeof(char*) [due to the implicit conversion / t 23> to rvalue conversion / automatic decay to pointer type] not 100*sizeof(char)

sizeof(arr) would give 100*sizeof(char) from 6.5.3.4/3

95). The comma operator does not give an lvalue.


decided that this would be another solution to the above problem that I tried in Microsoft Visual Studio 2008, but regardless of whether it is compiled as C or C ++ code sizeof (0, arr), it always turns out 4.

C ++ 03 [ 5.18/1 ] Comma Operator

The type and value of the result are the type and value of the right operand; the result is an lvalue if its right operand.

So sizeof(0, arr) = sizeof (arr) and which will be equal to 100* sizeof(char) and not = sizeof(char*) .

So, MSVC ++ gives the wrong result (in the case of C ++ code).

+1
source share

ISO C is the standard C. The current one is C99, but C1x is just around the corner. If fast, you mean a new standard every ten years or so, then yes, it is developing rapidly :-)

Section 6.5.3.4/3 of ISO C99 states:

When applied to an operand with type char, unsigned char or signed char (or its corresponding version), the result is 1.

When applied to an operand having an array type, the result is the total number of bytes in the array.

+5
source share

For arrays, sizeof returns the total size. Be careful with arrays passed as pointers.

C99 Standard:

When applied to an operand with an array of type, the result will be the total number of bytes in the array

0
source share

All Articles