gpt4 book ai didi

c - 传递给函数后打印数组时出现段错误,请解释行为

转载 作者:行者123 更新时间:2023-11-30 18:55:14 27 4
gpt4 key购买 nike

因此,我尝试将字符串数组(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);
}

最佳答案

这是因为getArguments()只是将指针的副本重新分配给其自身内部的字符指针。 main() 中的 arguments 未更新。

您应该将 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() 中按以下方式调用它。

cd_requested = getArguments(&arguments, command);

关于c - 传递给函数后打印数组时出现段错误,请解释行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28291405/

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