C: which means `int a [10]`

For example, I have this declaration:

int a[10]; 

Before that, I understood it as follows: a is actually a pointer, and it will point to 10 elements in sequence in memory.

But today, when my teacher taught me, he said: it will be an array of pointers, and each pointer points to its value.

I do not know what is true. please tell me.

+4
source share
5 answers

Prior to this, as I understand it: this is actually a pointer, and it will sequentially point to 10 elements in memory.

This is wrong, this is an array. It has a specific memory location and can contain 10 integers. With a pointer, you can do a = &some_int , however this does not work for arrays. If you pass a function to a waiting pointer, it will fade out (converted to) the pointer, but that's different.

But, today, when my teacher tach me, he said: he will have an array of pointer, and each pointer points to its value.

This is also not true, it is an array of 10 integers. To have 10 integer pointers, you need to define it as int *a[10] . However, the elements do not indicate their meanings.

+9
source

You and your teacher are wrong.

a will have some functions of a pointer to int , in which you can pass it to functions as a pointer and perform standard pointer arithmetic, but it is still an array in C terminology; you cannot change it (e.g. int * const ).

You are correct, however, that the elements a will be placed in memory as a sequential array, and not pointers to random places.

+6
source

I bet you misunderstood your teacher.

a is actually a pointer, and it will point to 10 elements in sequence in memory.

It is almost ok. (In some cases, you may think of it this way, but this is a big simplification, while others have explained why.)

it will be an array of pointers

This is completely wrong.

and each pointer indicates its value.

This is completely wrong.

+1
source

In simple theorems, β€œa” is a variable containing 10 elements in sequence in memory, and therefore we call it an array. To access the elements of a variable, an index is required. That is, to access the 5th element of the variable β€œa”, we need to specify [ 5]. The indication "a" indicates the starting address of the sequential memory cell and the indication + 5 points to the 5th element, starting with the first sequential memory.

0
source

You can find a good FAQ on arrays and pointers here: http://c-faq.com/aryptr/index.html .

int a[10]; refers to 10 integer cells allocated in memory.

int *b = a; equivalent to int *b = &a[0]; and means that b indicates that the first cell of a is accurate.

0
source

All Articles