gpt4 book ai didi

c - 用管道分隔数组

转载 作者:行者123 更新时间:2023-11-30 14:23:43 28 4
gpt4 key购买 nike

自上一个问题以来,我已经做了很多工作,所以我认为开始一个新问题以重新开始是合适的。如果这是错误的方法,请告诉我。 (我是新人!)

这是我的代码:

int do_command(char **args){

// In the end, this function should take an array of args
// and separate them into UNIX commands that lie before and after
// pipes that are entered and put them into an array called
// arrayOfCommands.
// For example, if the char array args comes in
// with {"ls", "-1", "|", "wc"}, there would be two commands
// which would be "ls -l" and "wc"

// The following number is in my real program,
// but using the contents of args above, there will be two
// commands ("ls -l" and "wc" separated by a pipe)
// The number is found dynamically by looking for the number
// of pipes earlier (in our examples so far, just one) and
// simply adding one since there has to be a command on either
// side of the pipe or an error is thrown.
const int commands = 2;

// I want to create an array that is 2 wide (int commands)
// and 5 tall (just a constant number because I don't know
// how many flags a user will enter after each UNIX command
//
// 0 1
// 0[][]
// 1[][]
// 2[][]
// 3[][]
// 4[][]
//
// Each column holds a UNIX command (Ex: "ls -s")
// In this example, "ls" would be stored in arrayOfCommands[0][0];
// And "-l" would be stored in arrayOfCommands[0][1];
//
// If there are more than one command (Ex: "ls -s | wc")
// They would be stored like this:
// "ls" in arrayOfCommands[0][0];
// "-l" in arrayOfCommands[0][1];
// "wc" in arrayOfCommands[1][0];

char arrayOfCommands[commands][5]; //!!! (Declaration)

//counts which column in arrayOfCommands 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 we find something that's not a pipe...
if(strcmp(args[i], "|")){
//put it into the array of commands
arrayOfCommands[commandNum][count] = args[i]; \\!!! (Copying)
count++;
}
//otherwise if we find a pipe...
else if (!strcmp(args[i], "|")) {
//we know we are done with that command
count = 0;
commandNum++;
}
//Looking at the next value in args
i++;
}

//Print the new arrayOfCommands
int col=0;
int row=0;
for (col=0;col<commands;col++){
printf("Command #%d: ",col);
for (row=0;row<10;row++){
printf("%c ",arrayOfCommands[col][row]); \\!!! (Printing)
}
printf("\n");
}
}

我疯狂地注释了代码,以帮助大家理解我的代码。为了进一步解释,我正在使用 C 创建一个 UNIX Shell。我的程序以用户输入的形式接收 UNIX 命令,并将它们放入名为“args”的数组中,并用空格分隔。

上面的代码专门与管道一起工作,并试图将代码进一步分成位于管道(“|”)之前和之后的命令。继续提问,我会尽力帮助解释我的思维过程。

我认为我的编码部分有些正确,但是当我尝试打印 arrayOfCommands 时,我得到类似的信息:

命令 0: % # ! ##$
命令1: * ^@ @!
(当然,这些是实际的符号)

我已经输入了“//!!!”我认为导致问题的原因。

最佳答案

如果你看:

char arrayOfCommands[commands][5]; //!!! (Declaration)
...
arrayOfCommands[commandNum][count] = args[i]; \\!!! (Copying)

您正在将 char* 分配给 char

C 字符串是字符数组。这意味着您有一个指向一系列以 NUL 字符结尾的 char 的指针。 args[i] 将返回该起始字符的内存地址。因此,您的“复制”只是将一个 unsigned int 内存地址分配给数组中指定为字符的元素。它将把 unsigned int (32 位)截断为“字符”(8 位)。这可能就是您看到奇怪值的原因。

如果您想按照您的指示存储命令,则必须将 arrayOfCommands 从二维 char 数组更改为二维 char * 数组。此外,如果您想要实际复制字符串,而不仅仅是为args[]数组中的每个字符串创建指针别名,则必须分配使用 malloc() 创建二维数组(然后在完成后使用 free() 释放内存),或者选择一个固定的最大大小并将其分配给堆栈。为了简单起见,我可能会在这样的程序中使用后者:

char arrayOfCommands[2][5][256];  // 5 rows, 2 columns, maximum command length of 255 characters + NUL character

然后,当你进行复制时,你将不得不使用字符串复制函数:

strcpy(arrayOfCommands[row][column], args[i]); // Copy string at args[i] into arrayOfCommands[row][column]

这应该可以解决您遇到的问题。

关于c - 用管道分隔数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608754/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com