c语言数组数组名_C数组简介

c语言数组数组名

An array is a variable that stores multiple values.

数组是存储多个值的变量。

Every value in the array, in C, must have the same type. This means you will have arrays of int values, arrays of double values, and more.

C中的数组中的每个值都必须具有相同的type 。 这意味着您将拥有int值数组, double值数组等等。

You can define an array of int values like this:

您可以像这样定义一个int值数组:

int prices[5];

You must always specify the size of the array. C does not provide dynamic arrays out of the box (you have to use a data structure like a linked list for that).

您必须始终指定数组的大小。 C没有提供开箱即用的动态数组(您必须使用数据结构,例如链表)。

You can use a constant to define the size:

您可以使用常量来定义大小:

const int SIZE = 5;
int prices[SIZE];

You can initialize an array at definition time, like this:

您可以在定义时初始化数组,如下所示:

int prices[5] = { 1, 2, 3, 4, 5 };

But you can also assign a value after the definition, in this way:

但是您也可以通过以下方式在定义之后分配一个值:

int prices[5];

prices[0] = 1;
prices[1] = 2;
prices[2] = 3;
prices[3] = 4;
prices[4] = 5;

Or, more practical, using a loop:

或者,更实际地,使用循环:

int prices[5];

for (int i = 0; i < 5; i++) {
  prices[i] = i + 1;
}

And you can reference an item in the array by using square brackets after the array variable name, adding an integer to determine the index value. Like this:

而且,您可以在数组变量名称之后使用方括号,并添加一个整数以确定索引值,从而引用数组中的项。 像这样:

prices[0]; /* array item value: 1 */
prices[1]; /* array item value: 2 */

Array indexes start from 0, so an array with 5 items, like the prices array above, will have items ranging from prices[0] to prices[4].

数组索引从0开始,因此包含5个项目的数组(如上面的prices数组)将具有从prices[0]prices[4]

The interesting thing about C arrays is that all elements of an array are stored sequentially, one right after another. Not something that normally happens with higher-level programming languages.

关于C数组的有趣之处在于,数组的所有元素都按顺序存储,一个接一个。 高级编程语言通常不会发生这种情况。

Another interesting thing is this: the variable name of the array, prices in the above example, is a pointer to the first element of the array, and as such can be used like a normal pointer.

另一个有趣的事情是:数组的变量名,即上例中的prices ,是指向数组第一个元素的指针 ,因此可以像普通指针一样使用。

More on pointers soon.

即将有更多关于指针的信息。

翻译自: https://flaviocopes.com/c-arrays/

c语言数组数组名

  • 20
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值