简体   繁体   English

字符串数组中的移位元素,C

[英]Shifting elements in array of strings, C

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];
        }

define historyArray as char *historyArray[HISTORY_DEPTH]; char *historyArray[HISTORY_DEPTH];定义为char *historyArray[HISTORY_DEPTH]; This defines historyArray as an array of character string pointers. 这将historyArray定义为字符串指针数组。 Then historyArray[0] points to teststring as a result of the assignment. 然后,由于分配, teststring [0]指向teststring 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. 并使用strcpy复制到该缓冲区。

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. 请注意,historyArray [0]的原始内容现在已丢失,如果使用malloc创建它,则将导致内存泄漏。 As a result, it should have had a free() applied to it. 结果,它应该已经应用了free() 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. 第二个字符串的strcpy会将historyArray指向的每个字符串的内容复制到前一个元素指向的缓冲区中,而无需移动指针本身。 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. 除非您输入空字符串,否则最后一个指针将继续保持与以前相同的数据。

Are you saying that if you have a string like 你是说如果你有一个像

aaa, bbb, ccc aaa,bbb,ccc

You want 你要

aaa, ccc, ccc aaa,ccc,ccc

as the result? 作为结果? Because your index starts at 1, which I suspect is not your intention. 因为您的索引从1开始,我怀疑这不是您的意图。 If it is the case, this can get you bbb, ccc, ccc using this 如果是这种情况,可以使用此功能获得bbb,ccc,ccc

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM