Jump to content
  • Advertisement

Archived

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

webmunkey

Help! Array Question...

This topic is 6188 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 );

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!