943,935 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4477
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 9th, 2007
0

Little Simple array question :)

Expand 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?
Last edited by laconstantine; May 9th, 2007 at 3:49 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 9th, 2007
0

Re: Little Simple array question :)

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 9th, 2007
0

Re: Little Simple array question :)

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 9th, 2007
0

Re: Little Simple array question :)

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?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 9th, 2007
0

Re: Little Simple array question :)

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 9th, 2007
1

Re: Little Simple array question :)

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!
Reputation Points: 29
Solved Threads: 2
Newbie Poster
iTaChi is offline Offline
21 posts
since Mar 2007
May 10th, 2007
0

Re: Little Simple array question :)

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 10th, 2007
0

Re: Little Simple array question :)

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 10th, 2007
0

Re: Little Simple array question :)

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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 10th, 2007
0

Re: Little Simple array question :)

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: sending login information to yahoo server
Next Thread in C Forum Timeline: help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC