C 中的命令数组
我正在尝试创建一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串复制到无效内存。
由于每次调用包装器
getLine()
都会获得新的内存缓冲区,因此您可以立即使用该缓冲区,而无需重复。使用完毕后,您需要释放所有存储的
history[]
行缓冲区。您没有在
getLine()
包装器中检查getline()
的返回状态。String copy to invalid memory.
As every call to wrapper
getLine()
will get you new memory buffer you can just use the buffer right away without duplication.You need to free all line buffers stored
history[]
once you're done with it.You're not checking the return status of
getline()
ingetLine()
wrapper.一个问题是您正在将行复制到 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.
你只需要复制该字符串
you just need to make a copy of the string