[Last Call] Learn how to a build a cloud-first strategyRegister Now


Easy thing...

Posted on 2006-05-11
Medium Priority
Last Modified: 2010-05-18
char *Array[10];
//here I put pointers in all 9 of Array
//excluded that code... tell me if thats necessary
for(unsigned int Pos = 0;Pos < 10;Pos++)//i want to move Array[1] to Array[0] and Array[2] to Array[1]... etc
Array[x] = Array[x+1];//i tryed with memcpy etc...
I think you understand...
Question by:Swemetal
1 Comment

Accepted Solution

steveiam earned 200 total points
ID: 16657157
Look into memmove().  It copes correctly with overlapping source and destinations.  memcpy()'s results are unpredictable.

  memmove(array, array+1, arraysize-1);  // as we're moving the data left one, theres only arraysize-1 elements to move

Otherwise, the code you have is nearly right apart from:
- your for loop useds "Pos", but your array indexes are "x"
- your upper bound on the for loop being one too high.  The "x+1" will fall off the end of the array.  
- (depening on the code that follows), you need to then deal with the last position as a special case.  

Also, using a constant for the array size is good practice, as it makes the code easier to understand, and can avoid problems later if you change the array size .. only change one number, rather than three.  If I wasn't using memmove, I'd do something like:

const int kArraySize = 10;
char *array[kArraySize];

for (unsigned int pos=0;pos<kArraySize-1;pos++) // don't try to move something into the last position

array[kArraySize-1] = 0; // fill in the new 'empty' position with a 0 (null) pointer

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question