Reputation: 30276
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.
Upvotes: 4
Views: 15419
Reputation: 105
Before, I understood it like this: a in fact is a pointer, and it will point to 10 elements consecutively in memory.
It's not right, you have misunderstood. Pointer and array are different.
// This is an array declaration
// requests that space for six int be set aside,
// to be known by the name ''a''.
// So, a is an array!
int a[10];
// This is a pointer declaration
// requests a place which holds a pointer, to be known by the name ''p''.
// p is a pointer!
int* p;
char a[] = "hello";
char *p = "world";
If you were to assign the array's address to the pointer:
p = a;
then p[3] and a[3] would access the same element.
This harmony of access explains how pointers can access arrays. BUT, pointer is not array.
WHY?
Because the compiler gets there differently.
It is useful to realize that a reference like x[3]
generates different code depending on whether x
is an array or a pointer. Given the declarations above, when the compiler
sees the expression a[3]
, it emits code to start at the location a
, move three past it, and fetch the character there. When it sees the expression p[3]
, it emits code to start at the location p
, fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to.
a[3]
is three places past (the start of) the object named a
, while p[3]
is three places past the object pointed to by p
. In the example above, both a[3]
and p[3]
happen to be the character 'l', but the compiler gets there differently.
Credit to:
http://c-faq.com/aryptr/aryptr2.html
http://c-faq.com/aryptr/aryptrequiv.html
Upvotes: 0
Reputation: 101
You can refer a nice FAQ on arrays and pointers here: http://c-faq.com/aryptr/index.html.
int a[10];
refers to 10 cells of integers allocated in memory.
int *b = a;
is equivalent to int *b = &a[0];
and means that b points to the first cell of a to be precise.
Upvotes: 1
Reputation: 4880
In simple terems "'a' is a variable that holds 10 elements consecutively in memory" and that's why we call it as array. Accessing elements of the variable requires index. i.e., to access 5th element of the variable 'a' we need to specify a[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.
Upvotes: 0
Reputation: 5411
I bet you've misunderstood your teacher.
a in fact is a pointer, and it will point to 10 elements consecutively in memory.
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.
Upvotes: 1
Reputation: 11784
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; you cannot change it for example (treat it something like 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.
Upvotes: 6
Reputation: 97948
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.
Upvotes: 10