传递函数后打印数组时出现段错误,请说明行为

时间:2015-02-03 04:32:39

标签: c arrays string

所以我试图将一个字符串数组(char **参数)传递给一个函数,用数值填充数组,然后在从函数返回后打印这些值。当我尝试打印"参数"的第一个值时,会出现问题。这给了我一个分段错误。为什么是这样?当我在" getArguments"中打印值时功能全部按预期进行。我是C的新手,是的,这是一项任务。我不是在找你为我编写这段代码,但是当我试图理解这个概念时,我想要解释这个行为。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define BUFFERSIZE 81
int getArguments(char** arguments, char* argument);
void getPath(char* pathBuffer);
int checkForDirectoryChange(char **arguments, int num_args);

int main(int argc, char *argv[]){
    char *  command;
    char ** arguments  = NULL;
    char *  cd_path;
    int len, pid, ret_code, cd_requested = 1;
    char buffer[BUFFERSIZE];


    /* Get user input and the first token */
    printf("Enter a command: > ");
    command = fgets(buffer,BUFFERSIZE,stdin);
    printf("The command entered was %s",buffer);


    len = strlen(buffer);
    if(buffer[len-1] == '\n')
        buffer[len-1]='\0';

    cd_requested = getArguments(arguments, command);


    printf("The argument passed is now: %s\n", arguments[0]);


    if(cd_requested == 0){
        fprintf(stdout,"Change directory requested.\n");
    }

    /*
    char * pathBuf;
    getPath(pathBuf);
    free the memory allocated */
    /*
    pid = fork();
    if(pid){
        wait(NULL);
    }else{

        ret_code = execvp(*arguments, arguments);
        if(ret_code){
            printf("The fork failed, exiting.");
            exit(0);
        }
    }*/

}

int getArguments(char** arguments, char* command){
    int n_spaces = 0,i;
    char *token;

    token = strtok(command, " ");
    /* Loop until we have gotten all of the tokens */
    while (token) {
        arguments = realloc (arguments, sizeof (char*) * ++n_spaces);

        if (arguments == NULL){
            printf("Memory allocation failed: token - %d\n", n_spaces);
            exit (-1); /* memory allocation failed */
        }
        arguments[n_spaces-1] = token;
        token = strtok (NULL, " ");
    }

    /* realloc one extra element for the last NULL */
    arguments = realloc (arguments, sizeof (char*) * (n_spaces+1));
    arguments[n_spaces] = 0;

    /* print the result */
    for (i = 0; i < (n_spaces+1); ++i)
        printf ("arguments[%d] = %s\n", i, arguments[i]);


    return strcmp("cd",arguments[0]);
}

int checkForDirectoryChange(char** arguments, int num_args){

    return 0;
}

void getPath(char* pathBuffer){
    size_t n;

    n = confstr(_CS_PATH, NULL, (size_t) 0);
    pathBuffer = malloc(n);
    if (pathBuffer == NULL)
        abort();
    confstr(_CS_PATH, pathBuffer, n);
}

1 个答案:

答案 0 :(得分:2)

这是因为getArguments()只重新指定了指向其内部字符指针的指针副本。 <{1}}中的arguments未更新。

您应该将main()定义为

getArguments()

并在int getArguments(char*** arguments, char* command) { /* ... */ while (token) { *arguments = realloc (*arguments, sizeof (char*) * ++n_spaces); if (*arguments == NULL){ printf("Memory allocation failed: token - %d\n", n_spaces); exit (-1); /* memory allocation failed */ } (*arguments)[n_spaces-1] = token; token = strtok (NULL, " "); } /* ... */ } 内将其称为以下内容。

main()