# Thread: Arrays and its decay

1. ## Arrays and its decay

Hi, crap title I know, but couldn't think of anything else.

Em, an array in its simplest is a collection of variables in a contiguous block..right?

So if I have
Code:
`int myarray[10] = {1,2,3,4,5,6,7,8,9} ;`
that declares the container...but I want to understand its pointer counterpart
myarray is a pointer to int* , when used on its own it decays..so I can do
Code:
`*(myarray+1)`
which is similar to
Code:
`myarray[1] ;`
Code:
`&myarray`
is a bit confusing however.
myarray doesn't decay to a pointer so it returns an address to a collection of 10 integers so its type is
Code:
`int(*)[10]`
I think..an address to the whole array.

but this is what I am trying to understand the most
&myarray[0] ;
This is why it was confusing me:

Code:
`char *person-name[]={"John","Doe","Purcell"}`
declares an array containing 3 pointers.
person-name on its own decays to a pointer to the first element, which in turn holds a pointer to the string "John"
&person-name returns a pointer to the whole array
Code:
`char(*)[3]`
I think?

but this
Code:
` &person-name[0]`
or
Code:
`&myarray[0]`
doesn't make sense to me.

Code:
`myarray[0]`
dereferences myarray to give me 1 so does it boil down to
Code:
`&1`
, where is 1 staying in memory? If is then it makes sense

and if
Code:
`person-name[0] = 0x344832`
for example does it boil down to
&0x344832 that really confused me lol..but it is saying where is this address or value stored in memory right? If so I think I get why
Code:
` &person-name[0]`
type is of
Code:
` char**`
as is person-name.
But in the case of &1 or &0x344832 for example, how does it know which 1 or 0x344832 it should look for if there were more than one 1's or 0x344832 in memory? Is there some kind of metadata attached to the value or something

Thanks...

2. foo[ X ] is one instance of whatever type foo is. So if that's an array of type int, then foo[ X ] gives you an integer. So &foo[ X ] gives you the address of an integer.

Quzah.

3. Hi quzah
So it would give me the address of whatever is in &foo[x] if foo[x] was 12 the address of 12 which is one int.

4. Originally Posted by Eman
Hi quzah
So it would give me the address of whatever is in &foo[x] if foo[x] was 12 the address of 12 which is one int.
&foo[12] is not the "address of 12," but the address of the 13th element of the array. foo[0] is the first element.

In general,

Code:
`foo[i] == *(foo + i)`
and

Code:
`&foo[i] == foo + i;`

5. No, it gives you the address of that spot in memory. Just like:
Code:
```int x = 5;
bar = &x;```
That doesn't give the address of the value 5. It gives you the address of the variable x, which just happens to contain the value of 5.
Code:
```int foo[ 2 ];
bar = &foo[ 1 ];```
That also gives you the address of an integer.

Quzah.

6. I said if foo[X] was 12 then &foo[X] translates to &12 which is the address of one int. But thanks anyways

Code:
`&foo[i] == foo + i;`
Is that right? Don't you mean
Code:
`&foo[i] == &(*(foo + i));`

7. Oh I see, so no dereferencing occurs. foo[0] is actually treated a "variable" a memory location