Different way of accessing array elements in C

前端 未结 8 2308
有刺的猬
有刺的猬 2020-11-30 04:49

I am a teaching assistant for a C programming course, and I came across the following line of C code:

char str[] = \"My cat\'s name is Wiggles.\";
printf(\"%         


        
相关标签:
8条回答
  • 2020-11-30 05:49

    It's a funky syntax for sure, but...

    str[5] would mean *(str+5)

    And

    5[str] would mean *(5+str)

    0 讨论(0)
  • 2020-11-30 05:51

    Perfectly valid C. From Wikipedia:

    Similarly, since the expression a[i] is semantically equivalent to *(a+i), which in turn is equivalent to *(i+a), the expression can also be written as i[a] (although this form is rarely used).

    Wacky, but valid.

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