Different way of accessing array elements in C

前端 未结 8 2346
有刺的猬
有刺的猬 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:38

    It's basically just the way C works. str[5] is really equivelent to *(str + 5). Since str + 5 and 5 + str are the same, this means that you can also do *(5 + str), or 5[str].

    It helps if you don't think of "5" as an index, but rather just that addition in C is commutative.

提交回复
热议问题