I've just read in the book "The C Programming Language" that array is not a variable and saw that assignment of array to pointers (and vice versa) can't be done as a result. So if array is not a variable then what is it?
-
1The following code is valid `int a[10]; int *p = a;`, but you can't do `a = p;`. – Jonathan Leffler Jun 09 '14 at 17:14
-
Arrays can't be assigned into at all, and when they are assigned into something else they decay to pointers. This is un-like true 1st class objects, which variables are. – StoryTeller - Unslander Monica Jun 09 '14 at 17:16
-
1also interesting read: http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c – Pavel Jun 09 '14 at 17:16
5 Answers
Array is a data structure containing a number of values, all of which have the same type and array names are non-modifiable l-values(named memory locations)-- it is addressable, but not modifiable. It means that it can't be modified or can't be the left operand of an assignment operator.
int a[10] = {0};
int *p = a; //OK
a++ // Wrong
a = p; // Wrong

- 104,019
- 25
- 176
- 264
int numbers[] = {1,2,3}
numbers
is not a variable, it is the array name which is nothing but the address of the first element in the array. To verifiy this, look at the address of numbers
and the address of numbers[0]
by doing this: printf("%p and %p and %p", &numbers, numbers, &numbers[0]);
All the the three pointers will have the same values since numbers
is nothing but the address of the first element in the array. Therefore numbers
is not a variable that contain a pointer or a value since it does not have a dedicated address in the memory to store value in it.
However, look at this pointer variable:
int *pnumbers = numbers;
`printf("%p and %p and %p", &pnumbers, pnumbers, &pnumbers[0]);`
You will notice that &pnumbers
has a different address in memory, and that's because pnumber
has a dedicated address in the memory where it stores the address of the first element in the array numbers
.
Putting the code all together:
#include <stdio.h>
main(){
int numbers[] = {1,2,3};
printf("%p and %p and %p\n", &numbers, numbers, &numbers[0]); // Address of numbers, value of numbers, first element of numbers
int *pnumbers = numbers;
printf("%p and %p and %p\n", &pnumbers, pnumbers, &pnumbers[0]); // Address of pnumbers, value of pnumbers, first element of the array pnumbers is pointing to
}
Output
0xbfb99fe4 and 0xbfb99fe4 and 0xbfb99fe4 // All three have the same address which is the address of the first element in the array
0xbfb99fe0 and 0xbfb99fe4 and 0xbfb99fe4 // The first one is different since pnumbers has been allocated a memory address to store a pointer which is the first element of the array numbers

- 7,733
- 4
- 28
- 53
It's a placeholder. A symbol to represent a commonly used method of referring to a sequential section of memory. It's not a variable all by itself.
int main(int argc, char** argv)
{
int array[10];
int value'
int* pointer;
value = array[0]; // Just fine, value and array[0] are variables
array[0] = value; // Just fine, value and array[0] are variables
pointer = &array[0]; // Just fine, &array[0] is an address
pointer = array; // Also just fine
//Because the compiler treats "array" all by itself as the address of array[0]
//That is: array == &array[0]
&array[0] = pointer // ERROR, you can't assign the address of something to something else.
array = pointer; // ERROR, array is not a variable, and cannot be assigned a value.
//Also bad, but technically they compile and could theoretically have their use
pointer = value;
pointer = array[0];
array[0] = pointer;
//Intermixing pointers and non-pointer variables is generally a bad idea.
}
array
is often treated like a variable because it represents the adddress of (the first item in) that block of memory. But it's not a variable. It doesn't have it's own memory to store anything. People set pointers equal to 'array' because it's a handy convention, compilers know what that means, and it's pretty common.

- 1,539
- 14
- 23
-
1`pointer = array[0]; // Just fine, array[0] is a variable`: No, this is not fine. `But it's not a variable.`: No. Its a variable. – haccks Jun 09 '14 at 17:53
-
What? yes it's a variaaaawhoa. What was I thinking... pointer = array[0] is not fine, because it's a variable and not an address. uuuuugggghh. – Philip Jun 09 '14 at 20:25
array is a sequence of elements of the same type. if you assign a pointer to an array, pointer will point to the address of first variable from an array.
int a[10];
int *p;
int *p2;
p = a;
p2 = &a[0];
printf("%d\n", p == p2);
output:
1

- 3,093
- 1
- 19
- 24
I've just read in the book "The C Programming Language" that array is not a
variable and saw that assignment of array to pointers (and vice versa)
Vice versa is allowed... An array is like a constant pointer (you cant change the address it is pointing to). However, you can assign that address to a pointer.
#include <stdio.h>
int main()
{
int x[3] = {1, 2, 3};
int *p = x;
p[0] = 50;
printf("%d", x[0]);
}

- 2,370
- 1
- 13
- 17
-
-
-
In C an array is storage for a number of objects, whereas a pointer indicates where another object may be found. Typically the pointer takes 4 or 8 bytes; the array may take megabytes – M.M Jun 09 '14 at 21:09
-
yes, but if you use just the identifier for the array, in an assignment for instance, you get address of its first element... In the code I've shown above, anything you do using the array x you can replace that for p... of course if you use sizeof the values will be different but what I meant is "treat an array just like a pointer whose address will never change" – vmp Jun 09 '14 at 21:14