简体   繁体   English

C:`int a [10]`是什么意思

[英]C: what does `int a[10]` mean

For example, I have this declaration : 例如,我有这个声明:

int a[10]; 

Before, I understood it like this: a in fact is a pointer, and it will point to 10 elements consecutively in memory. 在此之前,我理解为: a实际上是一个指针,它将在内存中连续指向10个元素。

But today, when my teacher taught me, he said: it will be an array of pointers, and each pointer points to its value. 但今天,当我的老师教我时,他说:它将是一个指针数组,每个指针都指向它的值。

I don't know which is true. 我不知道哪个是真的。 please tell me. 请告诉我。

Before, I understand like this : a in fact is a pointer, and it will points to 10 elements consecutively in memory. 在此之前,我理解为:a实际上是一个指针,它将在内存中连续指向10个元素。

This is wrong, it is an array. 这是错误的,它是一个数组。 It has a specific location in the memory and can hold 10 integers. 它在内存中有一个特定的位置,可以容纳10个整数。 With a pointer you can do a = &some_int , however, this does not work for arrays. 使用指针可以执行a = &some_int ,但是,这对数组不起作用。 If you pass a to a function that is expecting a pointer, it will be decayed (converted into) a pointer but this is different. 如果将a传递给期望指针的函数,它将被衰减(转换为)指针,但这是不同的。

But, today, when my teacher tauch me, he said : it will have an array of pointer, and each pointer point to its value. 但是,今天,当我的老师嘲笑我时,他说:它将有一个指针数组,每个指针都指向它的值。

This is also wrong, it is an array of 10 integers. 这也是错误的,它是一个由10个整数组成的数组。 To have 10 integer pointers, you need to define it as int *a[10] . 要有10个整数指针,需要将其定义为int *a[10] Still elements do not point to their values. 仍然元素不指向他们的价值观。

You and your teacher are both wrong. 你和你的老师都错了。

a will have some features of a pointer to int in that you can pass it to functions as a pointer and perform standard pointer arithmetic, but it still is an array in C terminology; a将具有指向int的指针的一些功能,你可以将它作为指针传递给函数并执行标准指针算术,但它仍然是C术语中的数组; you cannot change it for example (treat it something like int * const ). 你不能改变它(例如对待它像int * const )。

You are right, however, that the elements of a will be placed in memory as a consecutive array, not pointers to random places. 但是,你是对的, a的元素将作为连续数组放在内存中,而不是指向随机位置的指针。

I bet you've misunderstood your teacher. 我打赌你误会了你的老师。

a in fact is a pointer, and it will point to 10 elements consecutively in memory. a实际上是一个指针,它将在内存中连续指向10个元素。

This is almost ok. 几乎没问题 (In some cases you can think of it this way. But it's a great oversimplification, and others explained why.) (在某些情况下,你可以这样想。但这是一个很大的过度简化,其他人解释了原因。)

it will be an array of pointers 它将是一个指针数组

This is completely wrong. 这是完全错误的。

and each pointer points to its value. 并且每个指针指向其值。

This is completely wrong. 这是完全错误的。

In simple terems "'a' is a variable that holds 10 elements consecutively in memory" and that's why we call it as array. 在简单的terems中,“'a'是一个在内存中连续保存10个元素的变量”,这就是我们将其称为数组的原因。 Accessing elements of the variable requires index. 访问变量的元素需要索引。 ie, to access 5th element of the variable 'a' we need to specify a[5]. 即,要访问变量'a'的第5个元素,我们需要指定一个[5]。 specifying 'a' points to the starting address of the consecutive memory location and specifying a+5 points to the 5th element starting from the first consecutive memory. 指定'a'指向连续存储位置的起始地址,并指定从第一个连续存储器开始的第5个元素的+ 5个点。

You can refer a nice FAQ on arrays and pointers here: http://c-faq.com/aryptr/index.html . 您可以在这里引用关于数组和指针的精彩常见问题解答: http//c-faq.com/aryptr/index.html

int a[10]; refers to 10 cells of integers allocated in memory. 指的是在内存中分配的10个整数单元格。

int *b = a; is equivalent to int *b = &a[0]; 相当于int *b = &a[0]; and means that b points to the first cell of a to be precise. 并且意味着b指向a的第一个单元格是精确的。

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

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