简体   繁体   English

通过指针使用C中的数组吗?

[英]Arrays in C are used through pointers?

In a question I posted here: C++ - class issue 我在这里发布的一个问题是: C ++-类问题

One of the replies, which was from @SanSS mentioned the following part of the reply: 来自@SanSS的答复之一提到了答复的以下部分:

Arrays in C are used through pointers... C语言中的数组通过指针使用...

How is this done? 怎么做? And, can you clarify this by an example if possible? 并且,如果可能的话,您可以举例说明一下吗?

Thanks. 谢谢。

What is meant by that could be a couple of things: 这可能意味着两件事:

1) the subscript operator is defined in terms of pointer arithmetic. 1)下标运算符是根据指针算术定义的。 C99 6.5.2.1/2 "Array subscripting" says: C99 6.5.2.1/2“数组下标”说:

The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). 下标运算符[]的定义是E1 [E2]与(*((E1)+(E2)))相同。

As an example, assume you have an array declated like so: char s[] = "012345"; 例如,假设您有一个这样定义的数组: char s[] = "012345";

All of the following evaluate to '4': 以下所有值均评估为“ 4”:

  • s[4]
  • *(s + 4)
  • 4[s] - this unusual construct might surprise you, but because of the way that subscripting is defined by the standard, this is equivalent to *(4 + s) , which is the same as *(s + 4) and the same as s[4] . 4[s] -这种不寻常的构造可能会让您感到惊讶,但是由于标准定义了下标的方式,因此它相当于*(4 + s) ,与*(s + 4)相同,并且相同作为s[4]

2) (closely related to the above) array names evaluate to pointers to the first element of the array in most expressions (being the operand to the sizeof operation being the main exception). 2)(与上述关系密切)数组名称在大多数表达式中均指向指向数组第一个元素的指针(主要是对sizeof操作的操作数)。

pointer this link help you how to use array through pointer. 指针此链接帮助您如何通过指针使用数组。

sorry for my english. 对不起我的英语不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM