Array increment operator in C

前端 未结 7 510
暖寄归人
暖寄归人 2020-11-28 10:13

I don\'t understand the results of following code:

#include 
#include 
int main()
{
   int a[4]={1, 3, 5, 6};
   //suppose a is         


        
相关标签:
7条回答
  • 2020-11-28 10:57

    First this program invokes undefined behavior and I am little discouraged that with so many answers not one of them mentions this. In both your printf calls your argument is a pointer yet your are specifying the format as %d which expects and int it should be %p. The C99 draft standard in section 7.19.6.1 The fprintf function which printf's section refers back to for the format string paragraph 9 says:

    If a conversion specification is invalid, the behavior is undefined.[...]

    Back to your question, the a++ expression produces an error because postfix increment requires that it's operand is a modifiable lvalue, the draft standard in section 6.5.2.4 Postfix increment and decrement operators paragraph 1 says(emphasis mine):

    The operand of the postfix increment or decrement operator shall have qualified or unqualified real or pointer type and shall be a modifiable lvalue.

    we can see from setion 6.3.2.1 values, arrays, and function designators paragraph 1 says:

    [...]A modifiable lvalue is an lvalue that does not have array type[...]

    0 讨论(0)
提交回复
热议问题