![](/img/trans.png)
[英]Adding strings to an array of strings is somehow changing the previous strings in the array
[英]Why are the previous elements of my array changing (array of strings in C)
我正在构建一个外壳历史记录功能,以接收一个字符串,并在程序运行时将其添加到字符串数组中。
我的问题是,每当我用新行(字符串)更新数组时,缓存中的前一个元素就会被CWD(当前工作目录)填充,但是我需要保留设置它的前一个字符串。
这是我的主循环,获取字符串并尝试使用缓存功能存储历史记录:
//prints out the cwd; then loops to take in the line, split it up into arguments, and attempt to execute it
//while lsh_execute returns 0, then frees up the allocated space
void lsh_loop(void)
{
char *line; //pointer to a char (the beg. of an array of chars)
char *cache[10] = {NULL}; //history array
char **args; //pointer to a pointer of a char...
int status, counter = 0, i, j;
do {
printf("%s>", getcwd(0,0)); //print cwd
line = lsh_read_line(); //call read line
counter = lsh_cache_line(counter,line, cache);
printf("This is counter:%i\n", counter);
for(i=0; i<10; i++){
printf("This is cache[%i]:%s\n", i, cache[i]);
}
args = lsh_split_line(line); //split line
status = lsh_execute(args); //execute the split args
free(line); //free memory
free(args);
} while (status); //continue as long as execute returns 1
}
在此函数中,我将输入字符串行复制到字符串数组:
int lsh_cache_line(int counter,char *line, char *cache[10]){
(cache[counter]) = line;
printf("This is cache[%i]:%s\n", counter, cache[counter]);
counter++;
counter = counter % 10;
return counter;
}
这是我程序的输出:
paul@paul-VirtualBox:~/Desktop$ gcc shell.c
paul@paul-VirtualBox:~/Desktop$ ./a.out
/home/paul/Desktop>HI
This is cache[0]:HI
This is counter:1
This is cache[0]:HI
This is cache[1]:(null)
This is cache[2]:(null)
This is cache[3]:(null)
This is cache[4]:(null)
This is cache[5]:(null)
This is cache[6]:(null)
This is cache[7]:(null)
This is cache[8]:(null)
This is cache[9]:(null)
lsh: No such file or directory
/home/paul/Desktop>this is my problem
This is cache[1]:this is my problem
This is counter:2
This is cache[0]:/home/paul/Desktop
This is cache[1]:this is my problem
This is cache[2]:(null)
This is cache[3]:(null)
This is cache[4]:(null)
This is cache[5]:(null)
This is cache[6]:(null)
This is cache[7]:(null)
This is cache[8]:(null)
This is cache[9]:(null)
lsh: No such file or directory
/home/paul/Desktop>it overwrites my previous string with the cwd
This is cache[2]:it overwrites my previous string with the cwd
This is counter:3
This is cache[0]:/home/paul/Desktop
This is cache[1]:/home/paul/Desktop
This is cache[2]:it overwrites my previous string with the cwd
This is cache[3]:(null)
This is cache[4]:(null)
This is cache[5]:(null)
This is cache[6]:(null)
This is cache[7]:(null)
This is cache[8]:(null)
This is cache[9]:(null)
lsh: No such file or directory
/home/paul/Desktop>^C
paul@paul-VirtualBox:~/Desktop$
我尝试了各种声明和初始化字符串数组的方法,但是这种方法似乎效果最好。
我究竟做错了什么?
cache
没有存储字符串的空间。
诸如strdup
东西会创建存储,但是您稍后需要free
内存。
int lsh_cache_line(int counter,char *line, char *cache[10]){
(cache[counter]) = strdup(line);
printf("This is cache[%i]:%s\n", counter, cache[counter]);
counter++;
counter = counter % 10;
return counter;
}
字符串有10个插槽,但是字符串值没有存储空间。 您需要分配一些内存。
我敢打赌,您的lsh_read_line()函数将始终为您返回具有不同文本的相同缓冲区。 然后,将指向此缓冲区的指针存储到数组中的不同单元格中。 一旦将其输入到行变量中,就应该将文本复制到新分配的字符串中,并且以后只能使用此新副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.