我想将指向命令的指针存储到二维数组中。下面是我正在使用的一些代码,但我不知道如何获得所需的结果。
#define MAX_LINE_LENGTH 1024
#define MAX_COMMANDS 10
char *commandHistory[MAX_COMMANDS][MAX_LINE_LENGTH + 1];
char *token;
token = strtok(line, LSH_TOK_DELIM);
printf("%s\n", token); //prints out the command I want to store the pointer to
commandHistory[i++][0] = token; //what do I do here?
最佳答案
您需要分配字符串的副本并存储它。最简单的方法是这样的:
commandHistory[i++][0] = strdup(token);
当您的程序完成这些操作后,您应该释放
使用strdup
分配的每一个。
关于c - 如何将指针存储到二维数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482041/