簡體   English   中英

為什么我的數組的先前元素發生變化(C中的字符串數組)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM