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

• (2 Pages)
• 1
• 2

## 15 Replies - 1188 Views - Last Post: 11 February 2010 - 11:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=155519&amp;s=2a9454ad251d6183f281a01d9c960223&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mcclane400

Reputation: 5
• 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

• B&

Reputation: 150
• 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]

### #3 mcclane400

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

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

Posted 11 February 2010 - 09:26 PM

taylorc8, 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?

### #4 no2pencil

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• 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.

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 **

### #6 taylorc8

• B&

Reputation: 150
• 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

### #7 mcclane400

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

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

Posted 11 February 2010 - 09:43 PM

no2pencil, 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.

c.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?"

taylorc8, 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

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

### #9 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• 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?

### #10 mcclane400

Reputation: 5
• 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.

c.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?

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

### #12 mcclane400

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

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

Posted 11 February 2010 - 09:56 PM

ImaSexy, 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).

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

### #14 mcclane400

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

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

Posted 11 February 2010 - 10:05 PM

c.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.

c.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.

### #15 mcclane400

Reputation: 5
• 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?

c.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.