C 中的命令数组

发布于 2025-01-10 09:45:16 字数 1267 浏览 0 评论 0原文

我正在尝试创建一个名为 History 的 char** ,它包含如下命令数组:

do thing1
做事2
做事3
do thing4

这些命令由用户一次输入一个,我希望它们在出现时添加到这个历史数组中。在程序结束时,我想按照写入的顺序将命令打印回用户。

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int BUFFER = 50;
int MAX_ARGS = 100;
int HISTORY_SIZE = 50;

int bufferSize;

//Read in line from user 
char* getLine(void){

    char* line = NULL;
    size_t len = 0;
    size_t lineSize = 0;
    lineSize = getline(&line, &len, stdin);
    return line;
}

int main(){

    //declare variables
    char *line, *lineCopy;
   
    //history structure
    char* history[100];
    int historyCount = 0;

    do{

        //main program loop

        //get command 
        printf("# ");
        line = getLine();

        //copy command
        strcpy(lineCopy, line);

        history[historyCount] = lineCopy;
        historyCount++
        

    }while(historyCount < 4); //while status

    for(int i = 0; i < historyCount; i++){
        printf("%d: %s\n", i, history[i]);
    }

    return 0;
}

这样,数组中的每个值都设置为输入的最后一个命令,输出是

do thing4
做事4
做事4
do thing4

我明白这只是因为我每次都将它指向 lineCopy 的内存地址,但我不知道如何修复它并得到

do thing1
做事2
做事3
做事4

I am trying to create a char** called history that holds an array of commands like this:

do thing1
do thing2
do thing3
do thing4

The commands are input by the user one at a time and I want them to be added to this history array as they come. At the end of the program I want to print the commands back to the user in the order they were written.

Here is my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int BUFFER = 50;
int MAX_ARGS = 100;
int HISTORY_SIZE = 50;

int bufferSize;

//Read in line from user 
char* getLine(void){

    char* line = NULL;
    size_t len = 0;
    size_t lineSize = 0;
    lineSize = getline(&line, &len, stdin);
    return line;
}

int main(){

    //declare variables
    char *line, *lineCopy;
   
    //history structure
    char* history[100];
    int historyCount = 0;

    do{

        //main program loop

        //get command 
        printf("# ");
        line = getLine();

        //copy command
        strcpy(lineCopy, line);

        history[historyCount] = lineCopy;
        historyCount++
        

    }while(historyCount < 4); //while status

    for(int i = 0; i < historyCount; i++){
        printf("%d: %s\n", i, history[i]);
    }

    return 0;
}

With this, every value in the array is just set to the last command entered and the output is

do thing4
do thing4
do thing4
do thing4

I understand that this is just because I am pointing it to the memory address of lineCopy each time but I'm not sure how to fix it and get

do thing1
do thing2
do thing3
do thing4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赏烟花じ飞满天 2025-01-17 09:45:16
  1. 字符串复制到无效内存。

  2. 由于每次调用包装器 getLine() 都会获得新的内存缓冲区,因此您可以立即使用该缓冲区,而无需重复。

    //copy command
    //strcpy(lineCopy, line);

    history[historyCount] = line;
    historyCount++

    //or just
    history[historyCount++] = getLine();
  1. 使用完毕后,您需要释放所有存储的 history[] 行缓冲区。

  2. 您没有在 getLine() 包装器中检查 getline() 的返回状态。

  1. String copy to invalid memory.

  2. As every call to wrapper getLine() will get you new memory buffer you can just use the buffer right away without duplication.

    //copy command
    //strcpy(lineCopy, line);

    history[historyCount] = line;
    historyCount++

    //or just
    history[historyCount++] = getLine();
  1. You need to free all line buffers stored history[] once you're done with it.

  2. You're not checking the return status of getline() in getLine() wrapper.

七度光 2025-01-17 09:45:16

一个问题是您正在将行复制到 lineCopy,但您从未为 lineCopy 分配内存。

并且有一个由 100 个字符指针组成的数组,每个指针都指向 lineCopy,这实际上不是您想要的。因为 lineCopy 将始终指向您的情况中最新的字符串。这就是为什么历史记录中的每个命令本质上都是相同的。

One issue is you're copying the line over to lineCopy but you never allocated memory for lineCopy.

And have an array of 100 char pointers, each one pointer to lineCopy which actually isn't what you want. Because lineCopy will always point to the most recently string in your case. Which is why every command in your history essentially ends up being the same.

半枫 2025-01-17 09:45:16

你只需要复制该字符串

history[historyCount] = strdup(line);

you just need to make a copy of the string

history[historyCount] = strdup(line);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文