Little Simple array question :)

Thread Solved

Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Little Simple array question :)

 
0
  #1
May 9th, 2007
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?
Last edited by laconstantine; May 9th, 2007 at 3:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Little Simple array question :)

 
0
  #2
May 9th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Little Simple array question :)

 
0
  #3
May 9th, 2007
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Little Simple array question :)

 
0
  #4
May 9th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Little Simple array question :)

 
0
  #5
May 9th, 2007
>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:
  1. /* arr is not converted to a pointer */
  2. int arr[2] = {2, 3};
  3.  
  4. /* arr is converted to a pointer */
  5. printf ( "%d\n", arr[0] );
>What about "arr" location in memory
All of the following can and often do produce the same address:
  1. int arr[2] = {2, 3};
  2.  
  3. /* Address of the first element */
  4. printf ( "%p\n", (void*)&arr[0] );
  5.  
  6. /* Address of the array converted to a pointer */
  7. printf ( "%p\n", (void*)arr );
  8.  
  9. /* Address of the array as an object */
  10. 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'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 21
Reputation: iTaChi is an unknown quantity at this point 
Solved Threads: 2
iTaChi's Avatar
iTaChi iTaChi is offline Offline
Newbie Poster

Re: Little Simple array question :)

 
1
  #6
May 9th, 2007
Originally Posted by laconstantine View Post
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!
"If you understand, you can learn anything"


http://www.cprogrammingdock.serverspeople.net
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Little Simple array question :)

 
0
  #7
May 10th, 2007
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?
Last edited by laconstantine; May 10th, 2007 at 7:16 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Little Simple array question :)

 
0
  #8
May 10th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: Little Simple array question :)

 
0
  #9
May 10th, 2007
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?
Last edited by laconstantine; May 10th, 2007 at 12:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Little Simple array question :)

 
0
  #10
May 10th, 2007
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC