我有点麻烦,因为我是C新手,甚至不知道我想做什么是可能的。新阵列和指针
我将一个名为args的数组传递给函数。在函数内部,我还创建了一个名为arrayOfArgs的2D数组。我想要做的就是将参数中的某些值放入名为arrayOfArgs的二维数组中的特定位置。
这是我到目前为止的代码:
int do_command(char **args){
//this is usually a changing variable depending on the situation, but I've hard coded it to make sense
int commands = 3;
char **arrayOfArgs[commands][10];
//counts which column in arrayOfArgs we are on
int commandNum = 0;
//Counts which part of a command we are on
int count = 0;
//Array Counters
int i = 0;
int j;
//Go through args until we reach the end
while (args[i] != NULL){
if(!strcmp(args[i], "|")){
arrayOfArgs[commandNum][count] = args[i];
count++;
}
else if (strcmp(args[i], "|")) {
count = 0;
commandNum++;
}
//Looking at the next value in args
i++;
}
我遇到的问题,因为唯一的事情付诸arrayOfArgs是乱码。我确信我在数组指向的方式,arrayOfArgs的创建方式,或两者都做了错误。
或者是否有可能从一维到二维数组像我试图?
我敢肯定有一个NULL那里,因为前面我把这种循环,它的工作原理:
for(i = 0; args[i] != NULL; i++) {
printf("Argument %d: %s\n", i, args[i]);
}
谢谢!
我正在写一个C Shell,通过这种方式,我现在正在用管道编写部件。我想分开命令(用符号分隔)到二维数组中。 –
你是否明确地向'args'添加了最后一个'NULL'元素?如果你不是,那么你的循环条件是错误的,并不会正确终止:'args [i]!= NULL' –
我很确定有一个NULL,因为之前我调用一个函数(现在添加到主问题),直到NULL,并工作得很好。 –