Easy thing...

Posted on 2006-05-11
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
    LVL 3

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now