访问C中的数组元素的不同方式

时间:2021-11-09 21:29:51

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

我是C编程课程的助教,我遇到了下面一行C代码:

char str[] = "My cat's name is Wiggles.";
printf("%c %c %c %c\n", str[5], *(str + 5), *(5 + str), 5[str]);

I never came across the very last argument (5[str]) before, and neither did my professor. I don't think it's mentioned in K&R and C Primer Plus. I found this piece of code in a set of technical interview questions. Does anyone know why C allows you to access an array element that way also? I never heard of an index being outside the set of brackets and the name of an array inside the brackets.

我以前从未遇到过最后一场争论(5[str]),我的教授也没有。我不认为在K&R和C处理剂中提到过。我在一组技术面试问题中发现了这段代码。有人知道为什么C允许你用这种方式访问数组元素吗?我从来没有听说过索引在括号集合之外,在括号中有一个数组的名称。

Your help will be greatly appreciated!

非常感谢您的帮助!

7 个解决方案

#1


6  

Perfectly valid C. From Wikipedia:

完全有效的C.来自*:

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).

类似地,由于表达式a[i]在语义上等价于*(a+i),而*(i+a)又等价于*(i+a),因此表达式也可以写成i[a](尽管很少使用这种形式)。

Wacky, but valid.

古怪,但有效。

#2


2  

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).

类似地,由于表达式a[i]在语义上等价于*(a+i),而*(i+a)又等价于*(i+a),因此表达式也可以写成i[a](尽管很少使用这种形式)。

http://en.wikipedia.org/wiki/C_syntax#Accessing_elements

http://en.wikipedia.org/wiki/C_syntax Accessing_elements

#3


2  

Its all same. *ptr or ptr[0] actually means *(ptr+0). So whenever you write *ptr or ptr[0] it goes as *(ptr+0). Let say you want value at ptr[4] so it means you can also write it as *(ptr+4). Now whether you write it as *(ptr+4) or *(4+ptr), it's same. so just for understading if you can write *(ptr+4) as ptr[4] same way *(4+ptr) is same as 4[ptr]. Please go through http://en.wikipedia.org/wiki/C_syntax#Accessing_elements for more details.

它的所有相同。*ptr或ptr[0]实际上是指*(ptr+0)。所以无论什么时候写*ptr或ptr[0]它都是*(ptr+0)假设你想要在ptr[4]上的值,这意味着你也可以把它写成*(ptr+4)。不管你把它写成*(ptr+4)还是*(4+ptr)都是一样的。为了理解,如果你可以把*(ptr+4)写成ptr[4],就像*(4+ptr)等于4[ptr]请访问http://en.wikipedia.org/wiki/C_syntax#Accessing_elements以获得更多详细信息。

#4


2  

if str is an array of type char, then we can access any index say i as below-

如果str是char类型的数组,那么我们可以访问任何索引,如下面的i

  1. str[i]
  2. str[我]
  3. *(str + i)
  4. *(str + i)
  5. char *p = str, then access the index i as p[i] or *(p+i)
  6. char *p = str,然后访问索引i作为p[i]或*(p+i)

#5


1  

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].

它基本上就是C的工作方式。str[5]和*(str + 5)是等价的,因为str + 5和5 + str是相同的,这意味着你也可以做*(5 + str)或5[str]。

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

如果你不把“5”看成是一个指数,而是把C中的加法看成是可交换的。

#6


1  

str[5] directly translates to *(str + 5), and 5[str] directly translates to *(5 + str). Same thing =)

str[5]直接翻译为*(str + 5), 5[str]直接翻译为*(5 + str)。同样的事情=)

#7


0  

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

这是一种时髦的语法,但是…

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

str[5]意味着*(力量+ 5)

And

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

5(str)意味着*(5 + str)

#1


6  

Perfectly valid C. From Wikipedia:

完全有效的C.来自*:

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).

类似地,由于表达式a[i]在语义上等价于*(a+i),而*(i+a)又等价于*(i+a),因此表达式也可以写成i[a](尽管很少使用这种形式)。

Wacky, but valid.

古怪,但有效。

#2


2  

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).

类似地,由于表达式a[i]在语义上等价于*(a+i),而*(i+a)又等价于*(i+a),因此表达式也可以写成i[a](尽管很少使用这种形式)。

http://en.wikipedia.org/wiki/C_syntax#Accessing_elements

http://en.wikipedia.org/wiki/C_syntax Accessing_elements

#3


2  

Its all same. *ptr or ptr[0] actually means *(ptr+0). So whenever you write *ptr or ptr[0] it goes as *(ptr+0). Let say you want value at ptr[4] so it means you can also write it as *(ptr+4). Now whether you write it as *(ptr+4) or *(4+ptr), it's same. so just for understading if you can write *(ptr+4) as ptr[4] same way *(4+ptr) is same as 4[ptr]. Please go through http://en.wikipedia.org/wiki/C_syntax#Accessing_elements for more details.

它的所有相同。*ptr或ptr[0]实际上是指*(ptr+0)。所以无论什么时候写*ptr或ptr[0]它都是*(ptr+0)假设你想要在ptr[4]上的值,这意味着你也可以把它写成*(ptr+4)。不管你把它写成*(ptr+4)还是*(4+ptr)都是一样的。为了理解,如果你可以把*(ptr+4)写成ptr[4],就像*(4+ptr)等于4[ptr]请访问http://en.wikipedia.org/wiki/C_syntax#Accessing_elements以获得更多详细信息。

#4


2  

if str is an array of type char, then we can access any index say i as below-

如果str是char类型的数组,那么我们可以访问任何索引,如下面的i

  1. str[i]
  2. str[我]
  3. *(str + i)
  4. *(str + i)
  5. char *p = str, then access the index i as p[i] or *(p+i)
  6. char *p = str,然后访问索引i作为p[i]或*(p+i)

#5


1  

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].

它基本上就是C的工作方式。str[5]和*(str + 5)是等价的,因为str + 5和5 + str是相同的,这意味着你也可以做*(5 + str)或5[str]。

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

如果你不把“5”看成是一个指数,而是把C中的加法看成是可交换的。

#6


1  

str[5] directly translates to *(str + 5), and 5[str] directly translates to *(5 + str). Same thing =)

str[5]直接翻译为*(str + 5), 5[str]直接翻译为*(5 + str)。同样的事情=)

#7


0  

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

这是一种时髦的语法,但是…

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

str[5]意味着*(力量+ 5)

And

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

5(str)意味着*(5 + str)