开发者

easy c question: compare first char of a char array

开发者 https://www.devze.com 2023-01-01 18:30 出处:网络
How can I compare the first letter of the first element of a char**? I have tried: int main() { char** command = NULL;

How can I compare the first letter of the first element of a char**?

I have tried:

int main()
{
    char** command = NULL;
    while (true)
    {
        fgets(line, MAX_COMMAND_LEN, stdin);
        parse_command(line, command);
        exec_command(command);
    }
}

void parse_command(char* line, char** command)
{
    int n_args = 0, i = 0;
    while (line[i] != '\n')
    {
        if (isspace(line[i++]))
            n_args++;
    }

    for (i = 0; i <开发者_开发技巧; n_args+1; i++)
        command = (char**) malloc (n_args * sizeof(char*));

    i = 0;
    line = strtok(line," \n");
    while (line != NULL)
    {
        command[i++] = (char *) malloc ( (strlen(line)+1) * sizeof(char) );
        strcpy(command[i++], line);
        line = strtok(NULL, " \n");
    }
    command[i] = NULL;
}

void exec_command(char** command)
{
    if (command[0][0] == '/')
            // other stuff
}

but that gives a segmentation fault. What am I doing wrong?

Thanks.


Could you paste more code? Have you allocated memory both for your char* array and for the elements of your char* array?


The problem is, you do allocate a char* array inside parse_command, but the pointer to that array never gets out of the function. So exec_command gets a garbage pointer value. The reason is, by calling parse_command(line, command) you pass a copy of the current value of the pointer command, which is then overwritten inside the function - but the original value is not affected by this!

To achieve that, either you need to pass a pointer to the pointer you want to update, or you need to return the pointer to the allocated array from parse_command. Apart from char*** looking ugly (at least to me), the latter approach is simpler and easier to read:

int main()
{
    char** command = NULL;
    while (true)
    {
        fgets(line, MAX_COMMAND_LEN, stdin);
        command = parse_command(line);
        exec_command(command);
    }
}

char** parse_command(char* line)
{
    char** command = NULL;
    int n_args = 0, i = 0;
    while (line[i] != '\n')
    {
        if (isspace(line[i++]))
            n_args++;
    }

    command = (char**) malloc ((n_args + 1) * sizeof(char*));

    i = 0;
    line = strtok(line," \n");
    while (line != NULL)
    {
        command[i] = (char *) malloc ( (strlen(line)+1) * sizeof(char) );
        strcpy(command[i++], line);
        line = strtok(NULL, " \n");
    }
    command[i] = NULL;
    return command;
}

Notes:

  • in your original parse_command, you allocate memory to command in a loop, which is unnecessary and just creates memory leaks. It is enough to allocate memory once. I assume that you want command to contain n_args + 1 pointers, so I modified the code accordingly.
  • in the last while loop of parse_command, you increment i incorrectly twice, which also leads to undefined behaviour, i.e. possible segmentation fault. I fixed it here.
0

精彩评论

暂无评论...
验证码 换一张
取 消