I just was wondering why the array its self are a pointer to the first element?
and what is the difference between an array and a pointer?

>I just was wondering why the array its self are a pointer to the first element?
Because it was a design decision by the language creator.

>and what is the difference between an array and a pointer?
An array is a collection of objects, and a pointer is a variable whose value is an address. A pointer can of course point to any item in an array, or to the array itself. In value context, an array name is converted to a pointer to the first element. In object context, an array name refers to the array as a whole. The three object contexts are:

1) As the operand to sizeof
2) As the operand to the address-of operator
3) As a string constant initializer

Every other use of an array is in value context.

So when we declare an array here:

int arr[2] = {2,3};

we are taking 8 bytes of memory , then we are giving to 4 bytes of it the value 2 and the next 4 bytes of it the value 3.
then "arr" is converted to a pointer to the first element.

1.i got it so far, is it right??

2.What about "arr" location in memory i was checking its address and its address is the same as the address it is pointing to, so its means that "arr" is the first element and its points to its self?

>we are taking 8 bytes of memory , then we are giving to 4 bytes of it the
>value 2 and the next 4 bytes of it the value 3.
No. An int doesn't have to be 4 bytes. The statement is otherwise correct, but I would word it like this: Enough memory is set aside for two integers. The first integer is given the value 2 and the second integer is given the value 3.

>then "arr" is converted to a pointer to the first element.
No, the array name is only converted to a pointer in value context. That is, when you try to use it. This is a declaration, not a use. If you try to print the first item, that's value context and the array name is converted to a pointer:

/* arr is not converted to a pointer */
int arr[2] = {2, 3};

/* arr is converted to a pointer */
printf ( "%d\n", arr[0] );

>What about "arr" location in memory
All of the following can and often do produce the same address:

int arr[2] = {2, 3};

/* Address of the first element */
printf ( "%p\n", (void*)&arr[0] );

/* Address of the array converted to a pointer */
printf ( "%p\n", (void*)arr );

/* Address of the array as an object */
printf ( "%p\n", (void*)&arr );

This doesn't mean they're the same thing though. There's a difference in type between the address of the first element and the address of the array. The address of the first element iis a pointer to int and the address of the array is a pointer to an array of int. There's no "pointing to itself" going on.

I just was wondering why the array its self are a pointer to the first element?
and what is the difference between an array and a pointer?

nice question!

first, i want you to read a definition of arrays,

An array is an example of a homogeneous random access data structure. An array is merely a collection of similar data elements such as integers, floats, characters, etc. which is stored together with a common name, and addressed by means of index that tells the location of the particular data entity in the array..

When we use homogeneous to an array, it means that each cells have the same data type, and the random-access aspect tells that we can access any of the cells of an array directly by just using its indices.

for example, given is an array a[50], (note that an array begins in 0) in a[20] we assigned a value of 10 and in a[2] we assigned a value of 30. we can make use of this values simply by using the array's indices, for example,

a[20] + a[2] is similar to 10 + 30..


now, we can relate pointers to arrays..we can say that any cell of an array can be a pointer. an array can hold data value and its address..


good luck!

Comments
Good one.

I cant understand this

/* Address of the first element */
printf ( "%p\n", (void*)&arr[0] );

/* Address of the array converted to a pointer */
printf ( "%p\n", (void*)arr );

/* Address of the array as an object */
printf ( "%p\n", (void*)&arr );

If the address of the array is the same as its value, why?
I did the same with cout look

int arr[2] = {2,3};

cout << arr << endl << &arr;

The address of arr is the value of arr , why is that?

>I cant understand this
What a surprise.

>If the address of the array is the same as its value, why?
The address of the array is the memory location that the array starts at, right? The address of the first element of the array is also the memory location that the array starts at. Why shouldn't they be the same? The only difference is the resulting data type.

So the first element and the array its self are at same memory location,and the compiler treats the array as a pointer and as the TYPE its declared?

>So the first element and the array its self are at same memory location
Not necessarily. There's no guarantee that the address of the array as a whole is the same as the address of the first element. That's usually the case though.

>and when i use the array name the compiler treats its like a pointer , but
>its not a pointer right?
No, it is a pointer. When you use the array name in value context, the compiler actually converts it to a pointer to the first element. This is only the array name though, the array itself is never a pointer.

Too much complicated!!

int arr[2] = {2,3};
cout << sizeof(arr);

the output is 8, so how the hell arr can be a pointer to the first element (4bytes) if its 8 bytes size!!!

I dont understand anything can someone explain me how its works?

When you use the array name in value context, the compiler actually converts it to a pointer to the first element. This is only the array name though, the array itself is never a pointer.

Isn't this self explanatory?

at the place where the definition of the array is visible, sizeof will give the size of the entire array. it is when you use the name of the array in an expression that it is treated as a pointer to the first element.

#include <iostream>
int main()
{
  int a[20] = {0};
  std::cout << sizeof(a) << '\t' << sizeof(a+1-1) << '\n' ;
}

sizeof(a) here gives the sizeof the entire array.
a+1 is an expression, here a is treated as an int*.
note: when you try to pass the array to a function, it is used in an expression!
note2: a[3] is also an expression!, it means *(a+3); a is treated as a pointer to the first element
note3: a itself is not a pointer; it is converted to one. so, for example, trying to assign some value to a will make the compiler really angry.

so when we declare an array:

int arr[2] = {2,3};

we are taking some bytes "as a memory block"

then creating a pointer called 'arr' and pointing him to the block.

then when we using it in sizeof function
1.sizeof(arr);
its will print us the size of the whole block.
2.sizeof(arr+1);
its will get to the other element address and will print the size of whole the elements that is after the second.

thats right?

then when we using it in sizeof function
1.sizeof(arr);
its will print us the size of the whole block.

right!

2.sizeof(arr+1);
its will get to the other element address and will print the size of whole the elements that is after the second.
thats right?

no, that is not right. when you write arr+1, that is an expression. so arr is converted to an rvalue of type pointer to element of array. the result of the conversion is a pointer to the first element (at index zero). so arr+1 is really (pointer to first element) + 1; that is a pointer to the second element.
arr+1 == &(arr[1]) == &(arr[0])+1

so sizeof(arr+1) would give you the size of a pointer.

as per ISO/IEC 14882(E)
4.2 - Array-to-pointer conversion [conv.array]

-1- An lvalue or rvalue of type ``array of N T'' or ``array of unknown bound of T'' can be converted to an rvalue of type ``pointer to T.'' The result is a pointer to the first element of the array.

so when we declare an array:
int arr[2] = {2,3};
we are taking some bytes "as a memory block"
then creating a pointer called 'arr' and pointing him to the block.

i think you are getting it wrong here. arr is not a pointer, it is the array (the 'block' as you called it). it is just that arr can be implicitly coverted to an rvalue; a pointer which points to the first element of the block.

int arr[2] = { 2, 3 };
int* ptr = arr ; // ok, arr is converted to a pointer to first element
                      // equivalent to int* ptr = (int*)arr ;
int i = 8 ; arr = &i ; // not ok. arr is not a pointer. error.

>I dont understand anything can someone explain me how its works?
I did. Multiple ways, as simply as possible. You keep making the same mistake, which makes me think that you're not even trying to understand what you're told. This suggests one of three things:

1) You're not cut out for programming.
2) You're hoping that we'll stuff knowledge into your brain without any effort on your part.
3) You're trying to learn things that are too advanced for your present level.

I'm leaning toward 3 with a nice helping of 2. Why don't you slow down and try to learn some more beginner level topics?

part of the reason for you confusion is that you are trying out code with a. sizeof of the element (int) happens to be the same as the size of a pointer on your implementation. b. there are just two elements in the array.
try playing around with

double big_block[1000] ;

and try out your code on that array.

Okay i think i got it now.

int arr[2] = {2,3};
we are using a memory block to the first element we are giving the value 2 and the second 3.

The array name is used to represent the whole array (memory block) in 3 situations:
as narue said
1) As the operand to sizeof
2) As the operand to the address-of operator
3) As a string constant initializer

Every other use of an array is in value context.

Now a little question ;-)

If the array name is converted to a pointer then it should have a memory location??
But when i did this
cout << &arr << endl << arr[0];
they both have the same address.
Then what is wrong here?

>cout << &arr << endl << arr[0];
I assume you meant this:

cout << &arr << endl << &arr[0];

>Then what is wrong here?
Nothing. On your machine, &arr, and &arr[0] have the same address but they're treated as different entities because of a different data type. Let's say I have an array. I can tell the compiler that it's an int and as long as it behaves like an int when used like an int, the compiler doesn't know the difference. But I can still use it as an array if I want just by telling the compiler that it's an array. It's the same object, but the data type I use it as determines what it is to the compiler. Likewise, &arr is a pointer to an array, and &arr[0] is a pointer to an int. They both start at the same address, but the data type determines what they are to the compiler.

cout << &arr << endl << &arr[0];

why they both have the same address??

arr is the array not the first element?

>Not necessarily. There's no guarantee that the address of the array as a whole is the same as the address of the first element. That's usually the case though.

Its right, but you wouldnt call the array itself because they have different values, so the first value of the array is the same with the array itself if and only if the array only contains 1 value which is the first value..

>why they both have the same address??
Why do you keep asking the same question? Is it that difficult to understand that you can treat an address as multiple data types? If you don't get it yet, me explaining it umpteen more times probably won't help. So let's just say that this is how it is, and it really doesn't matter why it's that way yet.

>Its right, but you wouldnt call the array itself because they have different
>values, so the first value of the array is the same with the array itself if
>and only if the array only contains 1 value which is the first value..
What? Explain that a different way, because I didn't get what you're trying to say.

The array name is used to represent the whole array (memory block) in 3 situations:
as narue said
1) As the operand to sizeof
2) As the operand to the address-of operator
3) As a string constant initializer

here "&arr" its prints us the address of the whole array because of this:
2) As the operand to the address-of operator

this address is as same as the first element's address because the array starts at the first element so when we trying to get the array address its will give us the first element's address

Thats it??

dear laconstantine,
the name of the array stores the address of its first element. In the same manner, the pointer also points to the address of a variable. Both have the same function of referring to the address of a particular value in a particular address.
Coming to the difference between the pointer and an array,
1.The name of the array though it is a pointer, can point to a limited location in the memory.
Eg :in val[3]={10,20,30};
in the above example the array though it is a pointer, can point to the above listed three locations only, where as
2. A pointer can point to any location in the memory.
the above is the difference between an array and a pointer and also about array.

This question has already been answered. Start a new discussion instead.