Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在一个函数而不是另一个函数中访问同一内存时发生的分段错误_C_Struct_Linked List_Segmentation Fault - Fatal编程技术网

C 在一个函数而不是另一个函数中访问同一内存时发生的分段错误

C 在一个函数而不是另一个函数中访问同一内存时发生的分段错误,c,struct,linked-list,segmentation-fault,C,Struct,Linked List,Segmentation Fault,我正在实现一个LinkedList,其中包含一组ListNodes,这些ListNodes是包含一些关于人的基本信息的结构。我有两个文件,一个是main.c,另一个是main.h。每当我试图通过将根传递给函数来打印列表时,就会出现一个错误 这是主要的 #include "main.h" /* Edward Nusinovich This C file is going to have a LinkedList containing information about people.

我正在实现一个LinkedList,其中包含一组ListNodes,这些ListNodes是包含一些关于人的基本信息的结构。我有两个文件,一个是main.c,另一个是main.h。每当我试图通过将根传递给函数来打印列表时,就会出现一个错误

这是主要的

#include "main.h"

/* Edward Nusinovich

    This C file is going to have a LinkedList containing information about people.
    The user can interact with it and manipulate the list.

*/

int main(int argc, char **argv){

    if(!checkIfValidArguments(argc).value){return -1;}

    ListNode *root = askForDetails(1,argv);
    ListNode *current = root;
    current->next = NULL;

    ListNode *temp;

    int index = 2;

    while(index<argc){
        temp = askForDetails(index,argv);
        current->next = temp;
        current = current->next;
        index++;
    }

    userLoop(root);

    return 0;
}
#包括“main.h”
/*爱德华·努西诺维奇
此C文件将有一个LinkedList,其中包含有关人员的信息。
用户可以与之交互并操作列表。
*/
int main(int argc,字符**argv){
如果(!checkIfValidArguments(argc).value){return-1;}
ListNode*root=askForDetails(1,argv);
ListNode*当前=根;
当前->下一步=空;
ListNode*temp;
int指数=2;
而(indexnext=温度;
当前=当前->下一步;
索引++;
}
用户循环(根);
返回0;
}
在我的main.h文件中,在函数“userLoop”中打印root参数的属性没有问题,但是当我将ListNode*root传递给“print”或“stuff”时,我在打印其值时遇到了一个seg错误

以下是主要内容。h:

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

#define true 1
#define false 0

typedef struct bool{
    int value:1;
} boolean;

//this is going to store info about a person
typedef struct people{

    char *name;
    char *hairColor;
    char *eyeColor;
    char *age;
    struct people *next;    

} ListNode;

//using a 1 bit bitfield we have constructed, stores whether or not the user has entered a proper number of inputs
boolean checkIfValidArguments(int numArgs){

    boolean validArguments;

    if(numArgs>1){validArguments.value=true;}
    else{validArguments.value=false; printf("We need some names of people.\n");}

    return validArguments;
}

//this will construct a new Person and return him to 
ListNode *askForDetails(int personIndex, char **argv){

    ListNode toReturn;

    char *name = argv[personIndex];
    printf("Please enter the hair color, eye color, and age of %s.\n",name);

    char *inBuf=malloc(100);
    char nextchar=getchar();
    int index = 0;

    if(nextchar!='\n'){inBuf[index]=nextchar; index++;}

    while((nextchar=getchar())!='\n'){
        inBuf[index] = nextchar;
        index ++;
    }

    toReturn.name = name;
    toReturn.hairColor = strtok(inBuf," ");
    toReturn.eyeColor = strtok(NULL," ");
    toReturn.age = strtok(NULL,"\n");

    ListNode *newNode = malloc(sizeof(ListNode)); 
    newNode = &toReturn;    
    return newNode;
}

char *getInput(char *message){

    printf("%s",message);
    char *inBuf = malloc(40);
    char nextchar=getchar();
    int index = 0;
    if(nextchar!='\n'){inBuf[index]=nextchar; index++;}

    while((nextchar=getchar())!='\n'){
        inBuf[index] = nextchar;
        index ++;
    }

    return inBuf;

}

void addToEnd(ListNode *root){

    ListNode *current = root;

    char *inBuf = getInput("\nEnter the name of a person who you want to add: \n"); 

    ListNode *toAdd = malloc(sizeof(ListNode));
    toAdd = askForDetails(0,&inBuf);
    toAdd->name = inBuf;
    toAdd->next = NULL;

    while((current->next) != NULL){
        current = current->next;
    }

    current->next = toAdd;
}


void print(ListNode *root){

    printf("The name is %s.\n",root->name);
/*
    ListNode *current = root;

    do{
        printf("\n%s's hair is %s, their eyes are %s and they are %s years old.\n",current->name,current->hairColor,current->eyeColor,current->age);
        current = current->next;
    }while(current!=NULL);
*/  

}

void remEnd(ListNode *root){

    ListNode *current = root;

    if(current == NULL){ return; }





}

void addAfter(ListNode *root){

    ListNode *current = root;

    char *name = getInput("\nWho do you want to add after?\n");
    int comparison = 0; 

    while(current!=NULL&&(comparison = strcmp(name,current->name))!=0){
        current = current -> next;
    }

    if(current==NULL){printf("\nIndividual not found.\n"); return;}

    else{
        char *newPerson = getInput("What's the name of the person you wish to add? ");

        ListNode *toAdd = askForDetails(0,&newPerson);
        ListNode *next = current->next;
        current -> next = toAdd;
        toAdd->next = next;
        return;
    }

}

void stuff(ListNode *root){
    printf("name is %s.\n",root->name);
    printf("Root lives at %u.\n",root);
}

void userLoop(ListNode *root){

    char input = ' ';

    printf("Root lives at %u.\n",root);

    while(true){
        printf("name is %s.\n",root->name);     

        if(input!='\n'){
            printf("\nWhat would you like to do with your list:\n");
            printf("A) Add an element at the end of the list\n");
            printf("B) Remove an element from the end of the list\n");
            printf("C) Add an element after an element on your list\n");
            printf("D) Print your list\n");
            printf("E) Quit this program\n\n");
        }

        input = getchar();

        switch(input){
            case 'A': addToEnd(root); break;            
            case 'B': remEnd(root); break;
            case 'C': addAfter(root); break;
            case 'D': stuff(root); break;
            case 'E': return;
        }
    }

}
#包括
#包括
#包括
#定义真1
#定义false 0
类型定义结构布尔{
int值:1;
}布尔型;
//这将存储一个人的信息
typedef结构人{
字符*名称;
头发颜色;
字符*眼睛颜色;
字符*年龄;
结构人*下一步;
}列表节点;
//使用我们构建的1位位字段,存储用户是否输入了正确数量的输入
布尔校验有效值(整数){
布尔有效值;
如果(numArgs>1){validArguments.value=true;}
else{validArguments.value=false;printf(“我们需要一些人的名字。\n”);}
返回有效期;
}
//这将构造一个新的人,并将他返回到
ListNode*askForDetails(intPersonIndex,字符**argv){
列表节点返回;
char*name=argv[personIndex];
printf(“请输入%s的头发颜色、眼睛颜色和年龄。\n”,名称);
char*inBuf=malloc(100);
char nextchar=getchar();
int指数=0;
如果(nextchar!='\n'){inBuf[index]=nextchar;index++;}
而((nextchar=getchar())!='\n'){
inBuf[索引]=下一个目录;
索引++;
}
toReturn.name=名称;
toReturn.hairColor=strtok(inBuf,“”);
toReturn.eyeColor=strtok(NULL,“”);
toReturn.age=strtok(NULL,“\n”);
ListNode*newNode=malloc(sizeof(ListNode));
newNode=&toReturn;
返回newNode;
}
char*getInput(char*message){
printf(“%s”,消息);
char*inBuf=malloc(40);
char nextchar=getchar();
int指数=0;
如果(nextchar!='\n'){inBuf[index]=nextchar;index++;}
而((nextchar=getchar())!='\n'){
inBuf[索引]=下一个目录;
索引++;
}
返回inBuf;
}
void addToEnd(列表节点*根){
ListNode*当前=根;
char*inBuf=getInput(“\n输入要添加的人员的姓名:\n”);
ListNode*toAdd=malloc(sizeof(ListNode));
toAdd=askForDetails(0,&inBuf);
toAdd->name=inBuf;
toAdd->next=NULL;
while((当前->下一步)!=NULL){
当前=当前->下一步;
}
当前->下一步=添加;
}
作废打印(列表节点*根){
printf(“名称为%s.\n”,根->名称);
/*
ListNode*当前=根;
做{
printf(“\n%s的头发是%s,眼睛是%s,年龄是%s岁。\n”,当前->姓名,当前->头发颜色,当前->眼睛颜色,当前->年龄);
当前=当前->下一步;
}while(当前!=NULL);
*/  
}
无效重新命名(列表节点*根){
ListNode*当前=根;
if(current==NULL){return;}
}
void addAfter(列表节点*根){
ListNode*当前=根;
char*name=getInput(“\n您想在后面添加什么?\n”);
整数比较=0;
while(当前!=NULL&(比较=strcmp(名称,当前->名称))!=0){
当前=当前->下一步;
}
if(current==NULL){printf(“\nIndividualnotfound.\n”);return;}
否则{
char*newPerson=getInput(“您希望添加的人的名字是什么?”);
ListNode*toAdd=askForDetails(0,&newPerson);
ListNode*next=当前->下一步;
当前->下一步=添加;
toAdd->next=下一步;
返回;
}
}
无效内容(列表节点*根){
printf(“名称为%s.\n”,根->名称);
printf(“根目录位于%u.\n”,根目录);
}
void userLoop(ListNode*root){
字符输入=“”;
printf(“根目录位于%u.\n”,根目录);
while(true){
printf(“名称为%s.\n”,根->名称);
如果(输入!='\n'){
printf(“\n您想对列表做什么:\n”);
printf(“A)在列表末尾添加元素\n”);
printf(“B)从列表末尾删除元素\n”);
printf(“C)在列表中的元素之后添加元素\n”);
printf(“D)打印您的列表\n”);
printf(“E)退出此程序\n\n”);
}
input=getchar();
开关(输入){
案例“A”:addToEnd(根);break;
案例“B”:重新命名(根);中断;
案例“C”:addAfter(根);break;
案例“D”:填充(根);中断;
案例“E”:返回;
}
}
}
在两个函数中打印root的内存地址时,我得到的值相同,因此我不确定为什么我无法在一个函数中而不是在另一个函数中访问root中的值


非常感谢您的帮助。

我假设您使用的是64位系统,指针是8字节,整数是4字节。使用%p作为指针,而不是%u

我假设您在64位系统上,指针是8字节,整数是4字节。使用%p作为指针,而不是%u

您似乎不太了解C语言中的内存模型是如何工作的。您的错误代码可能是:

ListNode *newNode = malloc(sizeof(ListNode)); 
newNode = &toReturn;    
return newNode;
这将返回一个指向本地变量
的指针,而不是malloc的内存地址。您需要从