1

I want to shift each string in an array of strings one element to the left:

   char historyArray[HISTORY_DEPTH][COMMAND_LENGTH];

Neither of my two attempts below work. Can someone explain to me what I'm doing wrong?

for (int i = 1; i < HISTORY_DEPTH; i++) {
            strcpy(historyArray[i-1], historyArray[i]);
        }

for (int i = 1; i < HISTORY_DEPTH; i++) {
            historyArray[i-1] = historyArray[i];
        }
5
  • 1
    You do not declare historyArray as a type (I am guessing that it is an array of strings). You do not show how the array is initialized. Please show a full set that you try to run and show what goes wrong, not just the one line. Note that the second for loop refers to only the first index of a two D array. Commented Feb 12, 2016 at 1:55
  • 2
    Also, you might consider just writing a ring buffer (aka circular buffer) abstract data type. You can just change the pivot in O(1) time. Commented Feb 12, 2016 at 2:05
  • @sabbahillel I'm actually not sure how to initialize the array properly. An example of what I want to do: I have a string and I want to store it in historyArray[0]. I guess I can't do something like historyArray[0] = "teststring" because historyArray is 2D. Do I need to store the string character by character (i.e historyArray[0][0], historyArray[0][1]...)? Commented Feb 12, 2016 at 2:27
  • 1
    Your first attempt is correct (although it doesn't clear the last string, which probably would be a good idea). If your program is misbehaving then the error is in code you didn't show. Commented Feb 12, 2016 at 2:46
  • see my answer as to how to declare historyArray Commented Feb 12, 2016 at 18:08

2 Answers 2

2

define historyArray as char *historyArray[HISTORY_DEPTH]; This defines historyArray as an array of character string pointers. Then historyArray[0] points to teststring as a result of the assignment. As an array of pointers, you can handle each string pointer properly. You can then malloc a buffer pointer to use as an element in the array. and use strcpy to copy into that buffer.

char *historyArray[HISTORY_DEPTH];
// put initialization code here
for (int i = 1; i < HISTORY_DEPTH; i++) {
    historyArray[i-1] = historyArray[i];
}
historyArray[HISTORY_DEPTH-1] = NULL; //empty the last element pointer

This now moves the pointers into the previous element of the array.

Note that the original contents of historyArray[0] are now lost which would cause a memory leak if you had used malloc to create it. As a result, it should have had a free() applied to it. If it was a fixed buffer and does not need to be freed then you would not have to worry about it.

char historyArray[HISTORY_DEPTH][MAX_SIZE];
for (int i = 1; i < HISTORY_DEPTH; i++) {
    // Or use the memset with strlen(size+1)
    // to ensure that the ending '\0' is also copied
    strcpy(historyArray[i-1], historyArray[i]);
}
historyArray[HISTORY_DEPTH-1][0] = '\0'; // make the last an empty string

The strcpy of the second, will copy the contents of each string pointed to by historyArray into the buffer pointed to by the previous element without moving the pointers themselves. This assumes that each buffer is large enough to hold the character string. The last pointer continues to also hold the same data as it did before unless you put in an empty string.

Sign up to request clarification or add additional context in comments.

Comments

0

Are you saying that if you have a string like

aaa, bbb, ccc

You want

aaa, ccc, ccc

as the result? Because your index starts at 1, which I suspect is not your intention. If it is the case, this can get you bbb, ccc, ccc using this

for (int i = 0; i < HISTORY_DEPTH-1; i++) {
    strcpy(historyArray[i], historyArray[i+1]);
}

3 Comments

Thanks for point that out. I actually set historyArray[i] after getting out of the loop (obviously I didn't include that part).
I just read your comments. If you want to initialize each string, you can just do a strcpy(historyArray[i],"") or do a memset(historyArray,0,HISTORY_DEPTHCOMMAND_LENGTH). You cannot do array assignments like historyArray[i] = historyArray[i+1] because they are of type char ()[COMMAND_DEPTH], rather than pointers of type char*.
Original question moved i into (i-1) so need to start loop at 1 and move it into 0. Your code moves i (starting at 0) into i+1 which is the reverse of what was wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.