C.利用结构分割断层 #包括 #包括 #包括 #定义MAX_SUB_命令5 #定义最大参数10 结构子命令 { 字符*行; 字符*argv[MAX_ARGS]; }; 结构命令 { 结构子命令sub_命令[最大sub_命令]; int num_sub_命令; }; void ReadCommand(字符*行,结构命令**命令); 无效打印命令(结构命令**命令); void read_args(char*in,char*argv[MAX_args],int size); 无效打印参数(字符*参数[最大参数]); int main() { chars[200]; printf(“输入命令:”); fgets(s,200,stdin); s[strlen(s)-1]='\0'; struct命令*Command; ReadCommand(s,&command); PrintCommand(&command); } void ReadCommand(字符*行,结构命令**命令) { const char*delim=“|”; 常量字符*k; 字符*副本; char*l; int i=0; (*命令)->num_sub_命令=0; k=strtok(直线,delim); while(k!=NULL) { l=strdup(k); (*命令)->sub_命令[i].line=l; printf(“(*命令)->sub_命令[%d]。行=%s,l=%s\n”,i,(*命令)->sub_命令[i]。行,l); i++; printf(“%d次迭代\n”,i); (*命令)->num_sub_命令++; printf(“%d个子命令”\n),(*command)->num\u sub\u命令); k=strtok(NULL,delim); } (*command)->sub_命令[i].line=NULL; 对于(i=0;(*命令)->sub_命令[i]。行!=NULL;i++) { copy=strdup((*command)->sub_命令[i].line); read_args(复制,(*命令)->sub_命令[i].argv,MAX_args); } } void read_args(char*in,char*argv[MAX_args],int size) { const char*del=“”; int i=0; 常量字符*k1; char*l1; k1=strtok(英寸,德尔); while(k1!=NULL) { l1=标准值(k1); argv[i]=l1; //printf(“argv[%d]=%s\n”,i,argv[i]); i++; //printf(“k1=%s\n”,k1); //printf(“l1=%s\n”,l1); k1=strtok(空,del); } argv[i]=NULL; } 无效打印命令(结构命令**命令) { int i; 对于(i=0;(*命令)->sub_命令[i]。行!=NULL;i++) { printf(“子命令[%d]=“%s'\n”,i,(*命令)->sub_命令[i].line); print_args((*command)->sub_命令[i].argv); } } 无效打印参数(字符*argv[最大参数]) { int i; 对于(i=0;argv[i]!=NULL;i++) { printf(“argv[%d]=“%s'\n”,i,argv[i]); } }

C.利用结构分割断层 #包括 #包括 #包括 #定义MAX_SUB_命令5 #定义最大参数10 结构子命令 { 字符*行; 字符*argv[MAX_ARGS]; }; 结构命令 { 结构子命令sub_命令[最大sub_命令]; int num_sub_命令; }; void ReadCommand(字符*行,结构命令**命令); 无效打印命令(结构命令**命令); void read_args(char*in,char*argv[MAX_args],int size); 无效打印参数(字符*参数[最大参数]); int main() { chars[200]; printf(“输入命令:”); fgets(s,200,stdin); s[strlen(s)-1]='\0'; struct命令*Command; ReadCommand(s,&command); PrintCommand(&command); } void ReadCommand(字符*行,结构命令**命令) { const char*delim=“|”; 常量字符*k; 字符*副本; char*l; int i=0; (*命令)->num_sub_命令=0; k=strtok(直线,delim); while(k!=NULL) { l=strdup(k); (*命令)->sub_命令[i].line=l; printf(“(*命令)->sub_命令[%d]。行=%s,l=%s\n”,i,(*命令)->sub_命令[i]。行,l); i++; printf(“%d次迭代\n”,i); (*命令)->num_sub_命令++; printf(“%d个子命令”\n),(*command)->num\u sub\u命令); k=strtok(NULL,delim); } (*command)->sub_命令[i].line=NULL; 对于(i=0;(*命令)->sub_命令[i]。行!=NULL;i++) { copy=strdup((*command)->sub_命令[i].line); read_args(复制,(*命令)->sub_命令[i].argv,MAX_args); } } void read_args(char*in,char*argv[MAX_args],int size) { const char*del=“”; int i=0; 常量字符*k1; char*l1; k1=strtok(英寸,德尔); while(k1!=NULL) { l1=标准值(k1); argv[i]=l1; //printf(“argv[%d]=%s\n”,i,argv[i]); i++; //printf(“k1=%s\n”,k1); //printf(“l1=%s\n”,l1); k1=strtok(空,del); } argv[i]=NULL; } 无效打印命令(结构命令**命令) { int i; 对于(i=0;(*命令)->sub_命令[i]。行!=NULL;i++) { printf(“子命令[%d]=“%s'\n”,i,(*命令)->sub_命令[i].line); print_args((*command)->sub_命令[i].argv); } } 无效打印参数(字符*argv[最大参数]) { int i; 对于(i=0;argv[i]!=NULL;i++) { printf(“argv[%d]=“%s'\n”,i,argv[i]); } },c,segmentation-fault,structure,C,Segmentation Fault,Structure,我有错,唐诺,为什么?有人能帮忙吗?? 我在devc++ide中运行时遇到了分段错误,当我在shell上运行带有优化标志O0的代码时,它工作正常。我在您的代码中没有看到任何malloc(和任何免费的) 在你的主要工作中,你可以做到: #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_SUB_COMMANDS 5 #define MAX_ARGS 10 struct SubCo

我有错,唐诺,为什么?有人能帮忙吗??
我在devc++ide中运行时遇到了分段错误,当我在shell上运行带有优化标志O0的代码时,它工作正常。

我在您的代码中没有看到任何malloc(和任何免费的)

在你的主要工作中,你可以做到:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SUB_COMMANDS 5
#define MAX_ARGS 10
struct SubCommand
{
char *line;
char *argv[MAX_ARGS];
};
struct Command
{
struct SubCommand sub_commands[MAX_SUB_COMMANDS];
int num_sub_commands;
};
void ReadCommand(char *line, struct Command **command);
void PrintCommand(struct Command **command);
void read_args(char *in, char *argv[MAX_ARGS], int size);
void print_args(char *argv[MAX_ARGS]);
int main()
{
    char s[200];
    printf("Enter Command:");
    fgets(s,200,stdin);
    s[strlen(s)-1]='\0';
    struct Command *command;
    ReadCommand(s,&command);
    PrintCommand(&command);
}
void ReadCommand(char *line, struct Command **command)
{
    const char *delim ="|";
    const char *k;
    char *copy;
    char *l;
    int i=0;
    (*command)->num_sub_commands=0;
    k=strtok(line,delim);
    while(k!=NULL)
    {
        l=strdup(k);
        (*command)->sub_commands[i].line=l;
        printf("(*command)->sub_commands[%d].line=%s,l=%s\n",i,(*command)->sub_commands[i].line,l);
        i++;
        printf("%d iteration\n",i);
        (*command)->num_sub_commands++;
        printf("%d number of subcommands\n",(*command)->num_sub_commands);
        k=strtok(NULL,delim);
    }
    (*command)->sub_commands[i].line=NULL;
    for(i=0;(*command)->sub_commands[i].line!=NULL;i++)
    {
        copy=strdup((*command)->sub_commands[i].line);
        read_args(copy,(*command)->sub_commands[i].argv,MAX_ARGS);
    }
}
void read_args(char *in, char *argv[MAX_ARGS], int size)
{
        const char *del =" ";
    int i=0;
        const char *k1;
        char *l1;
        k1=strtok(in,del);
            while(k1!=NULL)
            {
                l1=strdup(k1);
                argv[i]=l1;
                //printf("argv[%d]=%s\n",i,argv[i]);
                i++;
                //printf("k1=%s\n",k1);
                //printf("l1=%s\n",l1);
                k1=strtok(NULL,del);
            }
    argv[i]=NULL;
}
void PrintCommand(struct Command **command)
{
    int i;
    for(i=0;(*command)->sub_commands[i].line!=NULL;i++)
    {
        printf("subcommand[%d]='%s' \n",i,(*command)->sub_commands[i].line);
        print_args((*command)->sub_commands[i].argv);
    }
}
void print_args(char *argv[MAX_ARGS])
{
    int i;
    for(i=0;argv[i]!=NULL;i++)
    {
        printf("argv[%d]='%s' \n",i,argv[i]);
    }
}
你可以改变:

struct Command command; //I don't understand why you are using pointers here
ReadCommand(s,command);
PrintCommand(command);
与:

例如,PrintCommand可以是:

void ReadCommand(char *line, struct Command *command);
void PrintCommand(struct Command *command);

还记得在使用完
子命令
结构的
argv
时,要使用
free

空行和空格不仅仅用于分隔编译器的标记。格式化你的代码。也可以把它简化成一个“有人能帮忙吗?”。是的,您可以通过使用调试器找到seg故障发生的确切位置来帮助自己。我纠正了我的错误谢谢!!:)
void ReadCommand(char *line, struct Command *command);
void PrintCommand(struct Command *command);
void PrintCommand(struct Command *command)
{
    int i;
    for(i=0;command->sub_commands[i].line!=NULL;i++)
    {
        printf("subcommand[%d]='%s' \n",i,command->sub_commands[i].line);
        print_args(command->sub_commands[i].argv);
    }
}