Array manipulation

Discussion in 'C Programming' started by perez.angela7@googlemail.com, Aug 8, 2007.

  1. Guest

    Hi all,

    I'm a student who just started programming in C. I have the following
    question: if I want to shift the elements of an array and insert a new
    value as the last element, I use this syntax (for an array a of
    elements size):

    for (i=0; i < size-1; i++)
    a=a[i+1]
    a[size-1]=newValue;

    But it turns out that this works too:

    int *p_i;
    p_i=&a[1];
    a[size-1]=newValue;

    Now, the pointer method seems more efficient to me. But because I'm
    not an expert on C (just started last week), I would like to know your
    thoughts on this - which is the preferred method? am i right in
    thinking the pointer method is more efficient? Any thought, or
    pointers (no pun) very welcome.

    Thanks for your help
    Angela
     
    , Aug 8, 2007
    #1
    1. Advertising

  2. Army1987 Guest

    On Wed, 08 Aug 2007 12:09:05 +0000, perez.angela7 wrote:

    > Hi all,
    >
    > I'm a student who just started programming in C. I have the following
    > question: if I want to shift the elements of an array and insert a new
    > value as the last element, I use this syntax (for an array a of
    > elements size):
    >
    > for (i=0; i < size-1; i++)
    > a=a[i+1]
    > a[size-1]=newValue;
    >
    > But it turns out that this works too:
    >
    > int *p_i;
    > p_i=&a[1];
    > a[size-1]=newValue;

    Is there something missing? Assigning to p_i doesn't magically
    write to a.

    [snip]
    --
    Army1987 (Replace "NOSPAM" with "email")
    No-one ever won a game by resigning. -- S. Tartakower
     
    Army1987, Aug 8, 2007
    #2
    1. Advertising

  3. Guest

    On Aug 8, 1:21 pm, Army1987 <> wrote:
    > On Wed, 08 Aug 2007 12:09:05 +0000, perez.angela7 wrote:
    > > Hi all,

    >
    > > I'm a student who just started programming in C. I have the following
    > > question: if I want to shift the elements of an array and insert a new
    > > value as the last element, I use this syntax (for an array a of
    > > elements size):

    >
    > > for (i=0; i < size-1; i++)
    > > a=a[i+1]
    > > a[size-1]=newValue;

    >
    > > But it turns out that this works too:

    >
    > > int *p_i;
    > > p_i=&a[1];
    > > a[size-1]=newValue;

    >
    > Is there something missing? Assigning to p_i doesn't magically
    > write to a.
    >
    > [snip]
    > --
    > Army1987 (Replace "NOSPAM" with "email")
    > No-one ever won a game by resigning. -- S. Tartakower


    My apologies, last line should be p_i[size-1]=newValue;
    p_i is the shifted version of a.
    Thanks
     
    , Aug 8, 2007
    #3
  4. Richard Guest

    writes:

    > Hi all,
    >
    > I'm a student who just started programming in C. I have the following
    > question: if I want to shift the elements of an array and insert a new
    > value as the last element, I use this syntax (for an array a of
    > elements size):
    >
    > for (i=0; i < size-1; i++)
    > a=a[i+1]
    > a[size-1]=newValue;
    >
    > But it turns out that this works too:
    >
    > int *p_i;
    > p_i=&a[1];
    > a[size-1]=newValue;
    >
    > Now, the pointer method seems more efficient to me. But because I'm
    > not an expert on C (just started last week), I would like to know your
    > thoughts on this - which is the preferred method? am i right in
    > thinking the pointer method is more efficient? Any thought, or
    > pointers (no pun) very welcome.


    I used to think that back when I started C in about 1989 or so and have
    used it on and off since then. Coming from a predominantly assembler
    background I used to assume that pointers were more efficient. I really
    don't believe there is anything in it anymore - optimizing compilers
    often do a better job than us mere humans can ever come close to. Also,
    never underestimate the value of readability.

    However, anyone who doesn't understand

    while(*d++=*s++);

    Should probably not be programming on real code bases since, for me, it
    pretty much summarises most of what you need to know about regarding
    pointers and contents pointed to.

    As far as your code goes, I would stick with the array notation.

    a=a[i+1]

    is, IMO, much cleaner than

    *p_a=*(p_a+1)

    since the second brings up all sorts of alarm bells about whether 1 is 1
    byte or size of one of the elements p_a points to etc .... not everyone
    who programs c knows this stuff as off pat as the regulars here.
     
    Richard, Aug 8, 2007
    #4
  5. Eric Sosman Guest

    wrote:
    > On Aug 8, 1:21 pm, Army1987 <> wrote:
    >> On Wed, 08 Aug 2007 12:09:05 +0000, perez.angela7 wrote:
    >>> Hi all,
    >>> I'm a student who just started programming in C. I have the following
    >>> question: if I want to shift the elements of an array and insert a new
    >>> value as the last element, I use this syntax (for an array a of
    >>> elements size):
    >>> for (i=0; i < size-1; i++)
    >>> a=a[i+1]
    >>> a[size-1]=newValue;
    >>> But it turns out that this works too:
    >>> int *p_i;
    >>> p_i=&a[1];
    >>> a[size-1]=newValue;

    >> Is there something missing? Assigning to p_i doesn't magically
    >> write to a.

    >
    > My apologies, last line should be p_i[size-1]=newValue;
    > p_i is the shifted version of a.


    ... so it all boils down to `a[size] = newValue;'.
    This is fine if the array `a' has `size+1' or more
    elements, but it doesn't "shift" anything. All the
    original `a' elements are still present and in their
    original positions; nothing has happened to them.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Aug 8, 2007
    #5
  6. Richard <> writes:

    > writes:
    >
    >> Hi all,
    >>
    >> I'm a student who just started programming in C. I have the following
    >> question: if I want to shift the elements of an array and insert a new
    >> value as the last element, I use this syntax (for an array a of
    >> elements size):
    >>
    >> for (i=0; i < size-1; i++)
    >> a=a[i+1]
    >> a[size-1]=newValue;
    >>
    >> But it turns out that this works too:
    >>
    >> int *p_i;
    >> p_i=&a[1];
    >> a[size-1]=newValue;

    corrected to 'p_i[size-1] = newValue;' in a later posting
    >>

    <snip>
    > As far as your code goes, I would stick with the array notation.
    >
    > a=a[i+1]
    >
    > is, IMO, much cleaner than
    >
    > *p_a=*(p_a+1)


    Yes, but that is incidental. The OP's two versions are doing quite
    different things. They are not two ways of writing the same shift,
    one using pointer and the other using subscripts.

    --
    Ben.
     
    Ben Bacarisse, Aug 8, 2007
    #6
  7. Thad Smith Guest

    wrote:
    > On Aug 8, 1:21 pm, Army1987 <> wrote:
    >
    >>On Wed, 08 Aug 2007 12:09:05 +0000, perez.angela7 wrote:


    >>>I'm a student who just started programming in C. I have the following
    >>>question: if I want to shift the elements of an array and insert a new
    >>>value as the last element, I use this syntax (for an array a of
    >>>elements size):

    >>
    >>>for (i=0; i < size-1; i++)
    >>> a=a[i+1]
    >>>a[size-1]=newValue;


    That will work.

    >>>But it turns out that this works too:

    >>
    >>>int *p_i;
    >>>p_i=&a[1];
    >>>a[size-1]=newValue;

    >
    > My apologies, last line should be p_i[size-1]=newValue;
    > p_i is the shifted version of a.


    They are not equivalent. The first piece eliminates the prior a[0]
    value and appends newValue to the end. The second piece replaces
    a[size-1] with newValue, thus throwing away a different piece of data.

    You may want to use a circular buffer to keep the last size entries. It
    uses a fixed array size and has two pointers, a head and tail, which
    determine where the new item is placed and where to remove the oldest item.

    --
    Thad
     
    Thad Smith, Aug 8, 2007
    #7
  8. writes:

    > On Aug 8, 1:21 pm, Army1987 <> wrote:
    >> On Wed, 08 Aug 2007 12:09:05 +0000, perez.angela7 wrote:
    >> > Hi all,

    >>
    >> > I'm a student who just started programming in C. I have the following
    >> > question: if I want to shift the elements of an array and insert a new
    >> > value as the last element, I use this syntax (for an array a of
    >> > elements size):

    >>
    >> > for (i=0; i < size-1; i++)
    >> > a=a[i+1]
    >> > a[size-1]=newValue;

    >>
    >> > But it turns out that this works too:

    >>
    >> > int *p_i;
    >> > p_i=&a[1];
    >> > a[size-1]=newValue;

    >>
    >> Is there something missing? Assigning to p_i doesn't magically
    >> write to a.
    >>

    >
    > My apologies, last line should be p_i[size-1]=newValue;
    > p_i is the shifted version of a.


    The second version, does not really shift the array. It is very much
    faster (it writes only one element and is O(1) rather O(size)) but it
    is also dangerous. In fact, it is plain wrong if your original
    remark:

    "for an array a of elements size"

    is true. If there are 'size' elements in 'a', 'p_i[size-1]=newValue'
    writes beyond the end of the array. The result is the dreaded
    "undefined behaviour".

    You *can* use this quasi-shift method if you make sure that there is
    enough extra room in the array, but I'd be sure to put lot if
    comments in to alert readers to the rather risky technique.

    --
    Ben.
     
    Ben Bacarisse, Aug 8, 2007
    #8
  9. Richard Guest

    Ben Bacarisse <> writes:

    > Richard <> writes:
    >
    >> writes:
    >>
    >>> Hi all,
    >>>
    >>> I'm a student who just started programming in C. I have the following
    >>> question: if I want to shift the elements of an array and insert a new
    >>> value as the last element, I use this syntax (for an array a of
    >>> elements size):
    >>>
    >>> for (i=0; i < size-1; i++)
    >>> a=a[i+1]
    >>> a[size-1]=newValue;
    >>>
    >>> But it turns out that this works too:
    >>>
    >>> int *p_i;
    >>> p_i=&a[1];
    >>> a[size-1]=newValue;

    > corrected to 'p_i[size-1] = newValue;' in a later posting
    >>>

    > <snip>
    >> As far as your code goes, I would stick with the array notation.
    >>
    >> a=a[i+1]
    >>
    >> is, IMO, much cleaner than
    >>
    >> *p_a=*(p_a+1)

    >
    > Yes, but that is incidental. The OP's two versions are doing quite
    > different things. They are not two ways of writing the same shift,
    > one using pointer and the other using subscripts.


    Yes, I chose to comment on the style rather than the coding errors which
    I felt would be better covered by others and, indeed, was.
     
    Richard, Aug 8, 2007
    #9
  10. <> wrote in message
    news:...
    > On Aug 8, 1:21 pm, Army1987 <> wrote:
    >> On Wed, 08 Aug 2007 12:09:05 +0000, perez.angela7 wrote:
    >> > Hi all,

    >>
    >> > I'm a student who just started programming in C. I have the following
    >> > question: if I want to shift the elements of an array and insert a new
    >> > value as the last element, I use this syntax (for an array a of
    >> > elements size):

    >>
    >> > for (i=0; i < size-1; i++)
    >> > a=a[i+1]
    >> > a[size-1]=newValue;

    >>
    >> > But it turns out that this works too:

    >>
    >> > int *p_i;
    >> > p_i=&a[1];
    >> > a[size-1]=newValue;

    >>
    >> Is there something missing? Assigning to p_i doesn't magically
    >> write to a.
    >>
    >> [snip]
    >> --
    >> Army1987 (Replace "NOSPAM" with "email")
    >> No-one ever won a game by resigning. -- S. Tartakower

    >
    > My apologies, last line should be p_i[size-1]=newValue;
    > p_i is the shifted version of a.
    > Thanks


    memmove( a, &a[1], (size*sizeof (*a) ) );
    a[size-1] = newValue;
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Aero Stability and Controls Computing
     
    Fred Kleinschmidt, Aug 8, 2007
    #10
  11. Serve Lau Guest

    <> wrote in message
    news:...
    > Hi all,
    >
    > I'm a student who just started programming in C. I have the following
    > question: if I want to shift the elements of an array and insert a new
    > value as the last element, I use this syntax (for an array a of
    > elements size):
    >
    > for (i=0; i < size-1; i++)
    > a=a[i+1]
    > a[size-1]=newValue;
    >
    > But it turns out that this works too:
    >
    > int *p_i;
    > p_i=&a[1];
    > a[size-1]=newValue;
    >
    > Now, the pointer method seems more efficient to me. But because I'm
    > not an expert on C (just started last week), I would like to know your
    > thoughts on this - which is the preferred method? am i right in
    > thinking the pointer method is more efficient? Any thought, or
    > pointers (no pun) very welcome.


    I wouldnt worry at all about performance when you just started last week.
    Just learn the language and eventually you will develop a style based on
    what you like best.
     
    Serve Lau, Aug 8, 2007
    #11
  12. On Wed, 8 Aug 2007 14:27:45 GMT, "Fred Kleinschmidt"
    <> wrote:
    > <> wrote in message
    > news:...
    > > On Aug 8, 1:21 pm, Army1987 <> wrote:
    > >> On Wed, 08 Aug 2007 12:09:05 +0000, perez.angela7 wrote:

    <snips>
    > >> > int *p_i;
    > >> > p_i=&a[1];
    > >> > a[size-1]=newValue;

    > > My apologies, last line should be p_i[size-1]=newValue;

    >
    > memmove( a, &a[1], (size*sizeof (*a) ) );


    .... ( (size-1) * sizeof (*a) ) ...

    and the outer parens aren't needed (or common), nor are the ones
    around the (non-type) operand of sizeof (but those are common)

    #if OFFTOPIC == PERFORMANCE
    This isn't likely to be much faster than the explicit
    for i a = a[i+1] loop the OP originally wrote. It is still O(size),
    opposed to O(1) for the strategies that involve adjusting pointers,
    although the k may be modestly better.

    > a[size-1] = newValue;


    - formerly david.thompson1 || achar(64) || worldnet.att.net
     
    David Thompson, Aug 26, 2007
    #12
    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. Garfield

    String Array Manipulation Problem

    Garfield, Aug 27, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,817
    Michael Lang
    Aug 27, 2003
  2. Sheldon
    Replies:
    12
    Views:
    604
    Alex Martelli
    Jun 25, 2006
  3. Replies:
    2
    Views:
    555
    Dieter Lamberty
    Aug 10, 2006
  4. Replies:
    12
    Views:
    627
    Keith Thompson
    Oct 4, 2006
  5. Aram Ter-Sarkissov

    array manipulation-

    Aram Ter-Sarkissov, Aug 17, 2010, in forum: Python
    Replies:
    2
    Views:
    281
    Gregory Ewing
    Aug 22, 2010
Loading...

Share This Page