简体   繁体   中英

Why are the previous elements of my array changing (array of strings in C)

I am building a shell history function to take in a string and add it to an array of strings as the program runs.

My problem is that whenever I update the array with a new line (string) the previous element in cache gets filled with my CWD (current working directory), but I need to to keep the previous string I set it to.

This is my main loop that gets the string and attempts to store the history with the cache function:

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

}

In this function, I am copying the input string line to the array of strings:

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; 

}

This is the output of my program:

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$ 

I have experimented with different ways of declaring and initializing the array of strings but this way seems to work the best.

What am I doing wrong?

There is no storage for the strings in cache .

Something like strdup would create storage, but you would need to free the memory later.

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; 

}

There are 10 slots for strings, but no memory for the string values. You need to allocate some memory.

I would bet that your lsh_read_line() function returns for you always the same buffer with different texts. You store then the pointer to this buffer into different cells in your array. You should copy the text to newly allocated string once you get it into your line variable and work later only with this new copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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