为什么我的数组的前一个元素在变化(C中的字符串数组)

时间:2015-09-07 19:24:01

标签: c arrays string

我正在构建一个shell历史记录函数来接受一个字符串,并在程序运行时将其添加到字符串数组中。

我的问题是每当我用新行(字符串)更新数组时,缓存中的前一个元素都会被我的CWD(当前工作目录)填充,但是我需要保留前面的字符串,我将其设置为。 / p>

这是我的主循环,它获取字符串并尝试使用缓存函数存储历史记录:

//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$ 

我尝试了不同的方式来声明和初始化字符串数组,但这种方式似乎效果最好。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

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个插槽,但字符串值没有内存。你需要分配一些内存。

答案 1 :(得分:0)

我敢打赌你的lsh_read_line()函数会为你返回不同文本的缓冲区。然后将指向此缓冲区的指针存储到数组中的不同单元格中。一旦将文本输入到行变量中,您应该将文本复制到新分配的字符串,稍后只能使用此新副本。