gpt4 book ai didi

c - 为什么会出现段错误?

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:53 24 4
gpt4 key购买 nike

这段代码的目标是像“ls | wc”一样拆分输入,并像 argv[0] = ls, argv 1 一样显示它。 = 厕所它在我的 MacBook 上工作正常,但在 Linux 机器上失败。它显示段错误。仔细查了一个早上,还是不知道为什么。有人可以帮助我吗?

Linux 机器上的结果是: enter image description here

#include <stdio.h>
#include <string.h>
#include <stdlib.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 PrintArgs(char** argv){
int i = 0;
while (argv[i] != NULL){
printf ("argv[%d] = '%s' \n", i , argv[i]);
i++;
}
return;
}

void ReadArgs(char *in, char **argv, int size){
if (size <= 0){ // in case of size is negative or 0//
fprintf(stderr,"Please enter a positive size.\n");
return;
}
if (size == 1){ // if size is 1, directly return//
*argv = NULL;
return;
}
char *p;
int length = strlen(in);
//in[length - 1] = '\0';//
int count = 1; //count the number of element, it is 1 since NULL must be included//
p = strtok(in, " ");
char *buff = strdup(p);
count++;
*argv = p;
argv++;
free(buff);
if (p == NULL){
*argv = NULL;
return; // we need just output one element//
}
while ((p=strtok(NULL, " "))!= NULL && (count <= size - 1)) {
buff = strdup(p);
*argv = p;
argv++;
count++;
free(buff);
}
argv++;
*argv = NULL;
return;
}

int get_args(char *in, char **argv, int max_args){
char *p;
int length = strlen(in);
in[length - 1] = '\0';
p = strtok(in, "|");
char *buff = strdup(p);
*argv = p;
argv++;
free(buff);
if (p == NULL){
return 1; // we need just output one array //
}
int count = 1; // if p is not null, it means at lease we have one array//
while ((p=strtok(NULL, "|"))!= NULL && (count <= max_args - 1)) {
buff = strdup(p);
*argv = p;
argv++;
count++;
free(buff);
}
return count;
}
void ReadCommand(char *line, struct Command *command){
/*Split the line by "|", and store them in argv*/
int i;
char *argv[MAX_SUB_COMMANDS];
int number = get_args(line, argv, MAX_SUB_COMMANDS);
/*End of Split procedure*/
/*Stored into sub-command's line*/
if (number > MAX_SUB_COMMANDS){
number = MAX_SUB_COMMANDS;
}
for (i = 0; i < number; i++){
command->sub_commands[i].line = argv[i];
command->num_sub_commands = i;
ReadArgs(command->sub_commands[i].line, command->sub_commands[i].argv, MAX_ARGS); //populate all argv in SubCommand//
}
return;
}
void PrintCommand(struct Command *command){
int i;
for (i = 0; i < MAX_SUB_COMMANDS; i++){
PrintArgs(command->sub_commands[i].argv);
}
}

int main(){
char s[200];
char *argv[10];
int argc;
printf("Enter a string: ");
fgets(s, sizeof s, stdin);
struct Command a;
struct Command *command;
command = &a;
ReadCommand(s, command);
PrintCommand(command);
return 0;
}

最佳答案

为什么不尝试使用 argvargc?干净多了

#include<stdio.h>

int main(int argc, char *argv[]){
int i;
for(i=0;i<argc;i++){
printf("Argv[%d] = %s\n", i, argv[i]);
}
return 0;
}

关于c - 为什么会出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42236303/

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