If "x" is an array, why does "x" and "&x"

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 745 Views - Last Post: 11 February 2010 - 11:40 PM Rate Topic: -----

#1 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 07:50 PM

I have the following code:

int a = 5;
cout << a << " " << &a;



Obviously a prints out 5 and &a prints out the address of a. Now this code:

int * b = new int[2];
cout << b << " " << &b;



I get 2 different memory addresses as expected. However, if I do this:

int a[] = {5,6,7};
cout << a << " " << &a;



I get the exact same memory location. Why are a and &a the exact same address? I'm assuming it has something to do with the static allocation of a and the dynamic allocation of b. Any ideas?

This post has been edited by mcclane400: 11 February 2010 - 09:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: If "x" is an array, why does "x" and "&x"

#2 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:14 PM

&a[0]
Was This Post Helpful? 0
  • +
  • -

#3 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:26 PM

View Posttaylorc8, on 11 February 2010 - 08:14 PM, said:

&a[0]


Not sure if you didn't finish your thought? &a[0] prints out the same address as a and &a as expected. &a[0] is really no different than just a, no?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5349
  • View blog
  • Posts: 27,305
  • Joined: 10-May 07

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:30 PM

The address for the array of a starts at array element zero.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_c.user*


Reputation:

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:31 PM

cout << b << " " << &b;

b is a variable that holds a value of int * type (i.e. b - is a variable that holds an address of integer object) )
it has an address too
its address has type int **
Was This Post Helpful? 0

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:34 PM

oh wait, srry i meant cout << *a; // lmao
Was This Post Helpful? 0
  • +
  • -

#7 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:43 PM

View Postno2pencil, on 11 February 2010 - 08:30 PM, said:

The address for the array of a starts at array element zero.


I'm not sure how that answers this question: "Why are a and &a the exact same address?" In my example, b is also an array, albeit a dynamically allocated one, but b and &b print off different addresses.

View Postc.user, on 11 February 2010 - 08:31 PM, said:

cout << b << " " << &b;

b is a variable that holds a value of int * type (i.e. b - is a variable that holds an address of integer object) )
it has an address too
its address has type int **


Yes, I understand that about b. My question is "Why are a and &a the exact same address?"

View Posttaylorc8, on 11 February 2010 - 08:34 PM, said:

oh wait, srry i meant cout << *a; // lmao


What is in "a" is not my concern. I don't care about *a. My question is why "a" and "&a" are the exact same address.

This post has been edited by mcclane400: 11 February 2010 - 09:42 PM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_c.user*


Reputation:

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:50 PM

mcclane400 said:

Why are a and &a the exact same address?

because "a" is a name of the array and &a removes operation &
the address of any array is the address of its first element
and the name "a" converts to the &a[0] automatically
as taylorc8 mentioned

This post has been edited by c.user: 11 February 2010 - 09:52 PM

Was This Post Helpful? 0

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:50 PM

why do you expect them to be different?
Was This Post Helpful? 0
  • +
  • -

#10 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:54 PM

Another fun little bit:

int a[] = {1,2,3};
cout << a << " " << &a << endl;
cout << *a << " " << *(&a) << endl;



The second line prints off the exact same memory address. However, the third line prints off "1 0xff...". This makes sense because a is an "int *" whereas &a is an "int **". I just don't see why the int * and int ** are the same memory location.

View Postc.user, on 11 February 2010 - 08:50 PM, said:

mcclane400 said:

Why are a and &a the exact same address?

because "a" is a name of the array and &a removes operation &
the address of any array is the address of its first element
and the name "a" converts to the &a[0] automatically
as taylorc8 mentioned


So &a and &a[0] are the exact same statement? This is true for statically allocated arrays but not dynamically allocated arrays, yes?
Was This Post Helpful? 0
  • +
  • -

#11 Guest_c.user*


Reputation:

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:56 PM

in this case there is no int **
as I said the operation & removes from *(&a) because "a" is an array name
int a[3];
and
int *a;
differs
in the second case "a" is a variable name
Was This Post Helpful? 0

#12 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 09:56 PM

View PostImaSexy, on 11 February 2010 - 08:50 PM, said:

why do you expect them to be different?


I thought that there would be a location that "a" is stored in memory and "a" would contain a pointer to the first element of the array. So "a" is the memory location of a[0] whereas &a would be where a is located in memory. This is true for dynamically created arrays but not statically created arrays (apparently).
Was This Post Helpful? 0
  • +
  • -

#13 Guest_c.user*


Reputation:

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 10:00 PM

mcclane400 said:

So &a and &a[0] are the exact same statement? This is true for statically allocated arrays but not dynamically allocated arrays, yes?

yes, &a converts to "a"
a dynamically allocated array is not an array ;)
it has no name
it is just a block in memory where a pointer points to
Was This Post Helpful? 0

#14 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 10:05 PM

View Postc.user, on 11 February 2010 - 08:56 PM, said:

in this case there is no int **
as I said the operation & removes from *(&a) because "a" is an array name
int a[3];
and
int *a;
differs
in the second case "a" is a variable name


I've checked the gnu documentation for any information on this and haven't found any. Do you happen to know of a page I can just go to and read? Even any page about the differences of "array names" and "variable names" as you say.

View Postc.user, on 11 February 2010 - 09:00 PM, said:

mcclane400 said:

So &a and &a[0] are the exact same statement? This is true for statically allocated arrays but not dynamically allocated arrays, yes?

yes, &a converts to "a"
a dynamically allocated array is not an array ;)
it has no name
it is just a block in memory where a pointer points to


Excellent. Yes, the dynamic part you say makes sense, thanks. I don't know that &a literally converts to a since *a and *(&a) give me 2 different things but I think I know what you are saying.
Was This Post Helpful? 0
  • +
  • -

#15 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

Re: If "x" is an array, why does "x" and "&x"

Posted 11 February 2010 - 10:16 PM

If we have int a[4]; and a is the memory location of where the array starts, then what purpose does &a serve? a is already an int * so I can assign some kind of iterator to walk through it. Why would you ever need to have an int ** (ie &a) in this particular case?

View Postc.user, on 11 February 2010 - 08:56 PM, said:

in this case there is no int **
as I said the operation & removes from *(&a) because "a" is an array name
int a[3];
and
int *a;
differs
in the second case "a" is a variable name


I see why &a is not an int **. So I tried to do this:

int ** h;
h = &a;



Obviously doesn't compile since a is not an int **. However, changing h's declaration to int *h[3] gives me this error at compile time: incompatible types in assignment of 'int (*)[3]' to 'int* [3]'

I really shouldn't have asked this question.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2