Array manipulation

P

perez.angela7

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
 
A

Army1987

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]
 
P

perez.angela7

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]


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

Richard

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.
 
E

Eric Sosman

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.
 
B

Ben Bacarisse

Richard said:
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

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.
 
T

Thad Smith

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.
 
B

Ben Bacarisse

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.
 
R

Richard

Ben Bacarisse said:
Richard said:
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

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.
 
F

Fred Kleinschmidt

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]


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;
 
S

Serve Lau

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.
 
D

David Thompson

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top