Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

c

pointers

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.

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.

like image 683
hqt Avatar asked Dec 04 '22 15:12

hqt


1 Answers

Before, I understand like this : a in fact is a pointer, and it will points to 10 elements consecutively in memory.

This is wrong, it is an array. It has a specific location in the memory and can hold 10 integers. With a pointer you can do a = &some_int, however, this does not work for arrays. If you pass a to a function that is expecting a pointer, it will be decayed (converted into) a pointer but this is different.

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. To have 10 integer pointers, you need to define it as int *a[10]. Still elements do not point to their values.

like image 127
perreal Avatar answered Dec 28 '22 22:12

perreal