• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Help! Array Question...

## Recommended Posts

webmunkey    122
Hey, I was just wondering how I could "delete" an entry in an array and have the entries after it move their index number down a place to take the place of the deleted entry. Example: int array[3] = {1, 2, 3}; array[0] + 1 = x In this example x would equal 2... but how could I change my array so that it would look like this: delete_array entry 1 // now array looks like this array[3] = {2, 3} array[0] + 1 = x; Now x would equal 3... but how can I do this? Please help! It is extremely important for me to implement this feature so I can finish my game! Thanks... -Jesse

##### Share on other sites
cyberg    122
maybe you could use CArray from MFC if you use this

else what you have to do is a loop like that

for(i=delete_entry;i{
array=array[i+1];
}

and I dont think that you can do something like
array[0] + 1 = x;
it will give you an error...

cyberg

##### Share on other sites
cyberg    122
grrrrrrrr html tag!!!!

what I have to do to post my code correctly?

##### Share on other sites
Guest Anonymous Poster
this shifts everything down 1.
you can play around with this theme to create a general array
element deletor.

int t[] = {1,2,3};

int s = sizeof(t)-sizeof(int);
memcpy( t, &t[1], s-1 );

Sandman    2210

##### Share on other sites
Beer Hunter    712
webmunkey - The Anonymous Poster knows his stuff. memcpy is a good choice.

cyberg - To get the < and > characters on this board, you have to type &lt; and &gt; . It can seem annoying, but such is life when html is enabled.

Sandman - Webmunkey seems to only be learning the language. Give ''em a chance to understand the basics before doing anything more complicated. I know that linked lists aren''t really that hard, but they can seem complicated to a beginner.

##### Share on other sites
BeerNutts    4400
Actually, You can''t use memcpy in this situation. If you read in K&R, you''ll see you should use memmove when memory overlaps each other. You''ll see location &t[1] is a part of t, so you should use memmove. We had a problem here at work where someone was using memcpy and the source was part of the destination.

##### Share on other sites
jacksonh    122
I would highly reccomend Linked Lists, if you don''t know how to use them find a tutorial on them or get ahold of me.

##### Share on other sites
webmunkey    122
Thanks everyone! I'm not familiar with linked lists yet, those will come later, but I did use the Anonymous Poster's algoritm and it worked great after I changed it to what BeerNutts said. Thanks again,
-Jesse

Edited by - webmunkey on August 3, 2001 12:08:20 PM

##### Share on other sites
merlin9x9    174
Why does everyone always seem to forget the STL? Use vector or list.

##### Share on other sites
Guest Anonymous Poster
Am I stupid or is what webmunkey describing similar to what
my book calls a Q(queue).
You have an array and then there are a number of rules
describing how data may enter and leave and what may and
may not be done.
I thought it was a common programming exercise and that it
also mimicks the real stack in memory, assemblers, correct
me if I''m wrong.
Of course, a linked list will work, but since you started
out with an array you may want to keep an index number in
each node.

int array[X] = {1,2,3};
for(i=0;i<(X-1);i++){
array = array[i+1];
}
array[i]=0;
/* final result array[X] = {2,3,0} */

##### Share on other sites
Beer Hunter    712
BeerNutts - memcpy will screw you over if the start of the destination lies inside the source block. But for sliding data down an array, the start of the destination lies outside that block, so you''re safe to use it for that purpose. Try it out for yourself and see.

##### Share on other sites
BeerNutts    4400
Beer Hunter (BTW, nice name)

I'm not sure of the implementation of memcpy vs memmove in the standard libraries, but considering memmove is described as: "same as memcpy, except it works when objects overlap." would lead me to always use memove if two objects overlapped, regardless of destination or source start.

I guess I'm just conservative, but, for embedded systems, better safe than sorry.

Nutts

Edit:
I actually looked at the old code that caused the problem, and it is exactly like the above problem. I believe it was intermittent, but memcpy was the culprit. We changed this piece of code:
  memcpy( &gTaskList[i], &gTaskList[i+1], sizeof( tTaskEntry ) * gulNumTasks - 1 );

to this
  memmove( &gTaskList[i], &gTaskList[i+1], sizeof( tTaskEntry ) * gulNumTasks - 1 );