431,805 Members | 1,202 Online
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

# Array manipulation

 P: n/a 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[i]=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 '07 #1
11 Replies

 P: n/a 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[i]=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 Aug 8 '07 #2

 P: n/a On Aug 8, 1:21 pm, Army1987

 P: n/a pe***********@googlemail.com 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[i]=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[i]=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. Aug 8 '07 #4

 P: n/a pe***********@googlemail.com wrote: On Aug 8, 1:21 pm, Army1987 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 followingquestion: if I want to shift the elements of an array and insert a newvalue as the last element, I use this syntax (for an array a ofelements size):for (i=0; i < size-1; i++) a[i]=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 magicallywrite 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 es*****@ieee-dot-org.invalid Aug 8 '07 #5

 P: n/a pe***********@googlemail.com wrote: On Aug 8, 1:21 pm, Army1987 >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 followingquestion: if I want to shift the elements of an array and insert a newvalue as the last element, I use this syntax (for an array a ofelements size): >>>for (i=0; i < size-1; i++) a[i]=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 Aug 8 '07 #6

 P: n/a Richard Hi all,I'm a student who just started programming in C. I have the followingquestion: if I want to shift the elements of an array and insert a newvalue as the last element, I use this syntax (for an array a ofelements size):for (i=0; i < size-1; i++) a[i]=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 >> As far as your code goes, I would stick with the array notation. a[i]=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. Aug 8 '07 #7

 P: n/a pe***********@googlemail.com writes: On Aug 8, 1:21 pm, Army1987 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[i]=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 magicallywrite 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. Aug 8 '07 #8

 P: n/a Ben Bacarisse pe***********@googlemail.com writes: >>Hi all,I'm a student who just started programming in C. I have the followingquestion: if I want to shift the elements of an array and insert a newvalue as the last element, I use this syntax (for an array a ofelements size):for (i=0; i < size-1; i++) a[i]=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 >>> >As far as your code goes, I would stick with the array notation.a[i]=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. Aug 8 '07 #9

 P: n/a 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[i]=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 magicallywrite 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 Aug 8 '07 #10

 P: n/a

 P: n/a On Wed, 8 Aug 2007 14:27:45 GMT, "Fred Kleinschmidt" 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[i] = 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 Aug 26 '07 #12

### This discussion thread is closed

Replies have been disabled for this discussion.