views:

134

answers:

4

Suppose,

int numbers [20];
int * p;

I think this is statement is valid

p = numbers;

But this is not

numbers = p;

Because numbers is an array, operates as a constant pointer, and we cannot assign values to constants. So if we go by this then we cannot use *numbers while initializing the array?

+1  A: 

sizeof(int) returns the number of bytes used to store an int

sizeof(int*) returns the number of bytes used to store a pointer

To declare an initialise a constant array of ints you can use the following syntax:

int numbers[] = { 0, 1, 2, 3 };
Alex Deem
+1  A: 

sizeof(int) is the size of the data type, sizeof(int*) is the size of a pointer to the datatype.

you cannot assign p to numbers, because numbers is declared as a fixed length stack based int array, its not an int pointer(though it can be converted to one)

Necrolis
+4  A: 

int numbers [20]; int * p;

I think this is statement is valid

p = numbers;

Yes

But this is not

numbers = p;

Because numbers is an array, operates as a constant pointer, and we cannot assign values to constants.

numbers is not a constant pointer, it is a non modifiable lvalue so you cannot assign to it.

sizeof(int) returns the size of an integer on any particular implementation

sizeof(int*) returns the size of a pointer to an integer.

return type of sizeof() is size_t (unsigned)

Prasoon Saurav
A: 

In simple words

int numbers [20]; is an integer array

int * p; is pointer to an integer; p stores the address it is pointing to

numbers = p; is not possible ; both are of different type one is int and other is int*

However numbers[0] = *p; is possible ; provided p points at some valid address

alam
I think you mean `numbers[0] = *p`
Necrolis
@Necrolis My bad , thanks for pointing out
alam
@alam: and numbers is not an int it is an array of 20 ints.
kriss
@Kriss, Yes you are right but here I used word "type"
alam