What happens when you perform arithmetic on an Array Variable?

Discussion in 'C++' started by Mercy, Dec 4, 2006.

  1. Mercy

    Mercy Guest

    I guess my C++ is pretty darn rusty. I was just looking over sample
    C++ code for practice... and I'm kind of confused about this code
    fragment:


    int sector2[512];
    int i = 3;

    memset(sector2, 128+i, 512);
    memset(sector2+256, 255-i, 256);

    I ran the code frag in visual studios, so I know what it does. But ...
    I've don't remember ever seeing arithmetic done on an array variable
    before. What exactly does sector2+256 mean? Does it just kinda of
    change the starting memory address of the variable sector2?

    Any help would be appreciated :)

    -Mercy
    Mercy, Dec 4, 2006
    #1
    1. Advertising

  2. Mercy

    IR Guest

    Mercy wrote:
    > int sector2[512];
    > int i = 3;
    >
    > memset(sector2, 128+i, 512);
    > memset(sector2+256, 255-i, 256);
    >
    > I ran the code frag in visual studios, so I know what it does.
    > But ... I've don't remember ever seeing arithmetic done on an
    > array variable before. What exactly does sector2+256 mean? Does
    > it just kinda of change the starting memory address of the
    > variable sector2?


    It's rather pointer arithmetics than array arithmetics.

    sector2 decays to int* (a pointer to the first element of the array),
    then this pointer is added 256, which results in a pointer to the
    257th element of the array.

    --
    IR
    IR, Dec 4, 2006
    #2
    1. Advertising

  3. Mercy wrote:
    > I guess my C++ is pretty darn rusty. I was just looking over sample
    > C++ code for practice... and I'm kind of confused about this code
    > fragment:
    >
    >
    > int sector2[512];
    > int i = 3;
    >
    > memset(sector2, 128+i, 512);
    > memset(sector2+256, 255-i, 256);
    >
    > I ran the code frag in visual studios, so I know what it does. But
    > ... I've don't remember ever seeing arithmetic done on an array
    > variable before. What exactly does sector2+256 mean? Does it just
    > kinda of change the starting memory address of the variable sector2?


    'sector2' used in an expression *decays* into a pointer to the first
    element. Addition applied to it is the same as taking the address
    of the corresponding element. I.e., if you have

    T array[<somedim>]

    then

    array + i

    is equivalent to

    & ( array )

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 4, 2006
    #3
  4. Victor Bazarov wrote:
    > Mercy wrote:
    >> I guess my C++ is pretty darn rusty. I was just looking over sample
    >> C++ code for practice... and I'm kind of confused about this code
    >> fragment:
    >>
    >>
    >> int sector2[512];
    >> int i = 3;
    >>
    >> memset(sector2, 128+i, 512);
    >> memset(sector2+256, 255-i, 256);
    >>
    >> I ran the code frag in visual studios, so I know what it does. But
    >> ... I've don't remember ever seeing arithmetic done on an array
    >> variable before. What exactly does sector2+256 mean? Does it just
    >> kinda of change the starting memory address of the variable sector2?

    >
    > 'sector2' used in an expression *decays* into a pointer to the first
    > element. Addition applied to it is the same as taking the address
    > of the corresponding element. I.e., if you have
    >
    > T array[<somedim>]
    >
    > then
    >
    > array + i
    >
    > is equivalent to
    >
    > & ( array )


    (Well, that was probably wrong. I was trying to explain it in terms
    which would be easier for you, but I assumed to much. In fact, the
    expression 'array' also has 'array' decaying into a pointer and
    is interpreted by the compiler as '* (array + i)', i.e. dereference
    the pointer expression obtained by adding the index to the pointer
    to the first element of the array, the latter comes from conversion
    of the array name into a pointer in any expression, IOW those two
    are equivalent because for 'int*', &* is a no-op)

    The only time where 'array' won't decay into a pointer is when a true
    array is expected, like an argument of 'sizeof', 'typeid', '&', or
    when initialising a reference to an array of the same dimension.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Dec 4, 2006
    #4
  5. Mercy

    Mercy Guest

    Victor Bazarov wrote:
    > Victor Bazarov wrote:
    > > Mercy wrote:
    > >> I guess my C++ is pretty darn rusty. I was just looking over sample
    > >> C++ code for practice... and I'm kind of confused about this code
    > >> fragment:
    > >>
    > >>
    > >> int sector2[512];
    > >> int i = 3;
    > >>
    > >> memset(sector2, 128+i, 512);
    > >> memset(sector2+256, 255-i, 256);
    > >>
    > >> I ran the code frag in visual studios, so I know what it does. But
    > >> ... I've don't remember ever seeing arithmetic done on an array
    > >> variable before. What exactly does sector2+256 mean? Does it just
    > >> kinda of change the starting memory address of the variable sector2?

    > >
    > > 'sector2' used in an expression *decays* into a pointer to the first
    > > element. Addition applied to it is the same as taking the address
    > > of the corresponding element. I.e., if you have
    > >
    > > T array[<somedim>]
    > >
    > > then
    > >
    > > array + i
    > >
    > > is equivalent to
    > >
    > > & ( array )

    >
    > (Well, that was probably wrong. I was trying to explain it in terms
    > which would be easier for you, but I assumed to much. In fact, the
    > expression 'array' also has 'array' decaying into a pointer and
    > is interpreted by the compiler as '* (array + i)', i.e. dereference
    > the pointer expression obtained by adding the index to the pointer
    > to the first element of the array, the latter comes from conversion
    > of the array name into a pointer in any expression, IOW those two
    > are equivalent because for 'int*', &* is a no-op)
    >
    > The only time where 'array' won't decay into a pointer is when a true
    > array is expected, like an argument of 'sizeof', 'typeid', '&', or
    > when initialising a reference to an array of the same dimension.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks Victor :)

    Pointers and such get quite confusing don't they? I think I understand
    what you were trying to say. I guess I just need a little bit more
    practice. Lots of trial and error should make this all second nature
    to me!

    -Mercy
    Mercy, Dec 4, 2006
    #5
  6. On 2006-12-04 23:54, Mercy wrote:
    > Victor Bazarov wrote:
    >> Victor Bazarov wrote:
    >> > Mercy wrote:
    >> >> I guess my C++ is pretty darn rusty. I was just looking over sample
    >> >> C++ code for practice... and I'm kind of confused about this code
    >> >> fragment:
    >> >>
    >> >>
    >> >> int sector2[512];
    >> >> int i = 3;
    >> >>
    >> >> memset(sector2, 128+i, 512);
    >> >> memset(sector2+256, 255-i, 256);
    >> >>
    >> >> I ran the code frag in visual studios, so I know what it does. But
    >> >> ... I've don't remember ever seeing arithmetic done on an array
    >> >> variable before. What exactly does sector2+256 mean? Does it just
    >> >> kinda of change the starting memory address of the variable sector2?
    >> >
    >> > 'sector2' used in an expression *decays* into a pointer to the first
    >> > element. Addition applied to it is the same as taking the address
    >> > of the corresponding element. I.e., if you have
    >> >
    >> > T array[<somedim>]
    >> >
    >> > then
    >> >
    >> > array + i
    >> >
    >> > is equivalent to
    >> >
    >> > & ( array )

    >>
    >> (Well, that was probably wrong. I was trying to explain it in terms
    >> which would be easier for you, but I assumed to much. In fact, the
    >> expression 'array' also has 'array' decaying into a pointer and
    >> is interpreted by the compiler as '* (array + i)', i.e. dereference
    >> the pointer expression obtained by adding the index to the pointer
    >> to the first element of the array, the latter comes from conversion
    >> of the array name into a pointer in any expression, IOW those two
    >> are equivalent because for 'int*', &* is a no-op)
    >>
    >> The only time where 'array' won't decay into a pointer is when a true
    >> array is expected, like an argument of 'sizeof', 'typeid', '&', or
    >> when initialising a reference to an array of the same dimension.
    >>
    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask

    >
    > Thanks Victor :)
    >
    > Pointers and such get quite confusing don't they? I think I understand
    > what you were trying to say. I guess I just need a little bit more
    > practice. Lots of trial and error should make this all second nature
    > to me!


    For most usages of C++ you don't have to use pointers at all, even less
    so arrays. C++ has references and vector<>, which will often do the jub
    just as well but are much safer and easier to use.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Dec 5, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,870
    Toby Inkster
    Aug 15, 2004
  2. Moose
    Replies:
    6
    Views:
    15,336
    Alfonso Esteban Perdigón
    Aug 17, 2004
  3. joshc
    Replies:
    5
    Views:
    556
    Keith Thompson
    Mar 31, 2005
  4. NM
    Replies:
    6
    Views:
    465
    Default User
    Sep 20, 2006
  5. Christopher
    Replies:
    9
    Views:
    612
    Marcel Müller
    Nov 6, 2012
Loading...

Share This Page