Arrays and its decay

This is a discussion on Arrays and its decay within the C Programming forums, part of the General Programming Boards category; Hi, crap title I know, but couldn't think of anything else. Em, an array in its simplest is a collection ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    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:
    if I had this array

    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...
    You ended that sentence with a preposition...Bastard!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    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.
    You ended that sentence with a preposition...Bastard!

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    320
    Quote Originally Posted by Eman View Post
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    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));
    You ended that sentence with a preposition...Bastard!

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Oh I see, so no dereferencing occurs. foo[0] is actually treated a "variable" a memory location
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is the datatype? -- possible array decay?
    By steals10304 in forum C Programming
    Replies: 2
    Last Post: 10-12-2009, 09:28 PM
  2. corruption, decay, Vista Gateway laptop
    By CodeMonkey in forum Tech Board
    Replies: 5
    Last Post: 06-13-2009, 01:50 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  5. Replies: 2
    Last Post: 02-23-2004, 05:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21