So I am asked to develop a program that makes a family tree. As one of the first functions to do, is a function that finds if the input name has been already added to a person. my add function works fine but when I try to use the findPerson function, I get the error of Segmentation Fault (core dumped).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct _person {
char *name;
int birth;
struct _person *nextperson;// pointer to the next person structure on
struct _person *mother; //mother pointer to another person structure
struct _person *father;//father pointer to another person structure
} person;
void addperson (person **families, char *name, int birthyear)
{
person *newperson;
if (strcmp(name,"unknown")!=0) {
newperson=(person *)malloc(sizeof(person));//allocate space for the n
if (newperson==NULL) {
printf("insert: error: no space left\n");
return;
}
//make space for newperson's name, the lenght of the input name
newperson->name =(char *)malloc(sizeof(char)*(strlen(name) + 1));
strcpy(newperson->name, name);//copy the input argument name to the
newperson->birth=birthyear;
newperson->nextperson=*families;
newperson->mother=NULL;
newperson->father=NULL;
return;
} else {
printf("A person can not be called unknown\n");
}
}
person *findperson (person *families, char *person)
{
struct _person *finder= families;
// finder =(person *)malloc(sizeof(person));
//finder=families;
if(strcmp(finder->name,person)==0) {
return finder;
} else {
if (finder->nextperson!=NULL) {
finder=finder->nextperson;
findperson(finder, person);
} else {
printf("Person %s not found",person);
}
}
}
void main(){
person *families= NULL; //initializ list with 0
char*name,*name2;
char command;
person * foundPerson;
int birth;
int loop=1;
while(loop==1){
printf("command?");
scanf(" %c", &command);
switch(command){
case 'q':
printf("bye\n");
loop=2;
break;
// case '\n':
// break;
case 'i':
printf("name? ");
scanf(" %[^\n]s", name);
printf("birthyear? ");
scanf(" %d", &birth);
addperson(&families, name, birth);
break;
case 'f':
printf("name? ");
scanf(" %[^\n]s", name2);
foundPerson= findperson(families,name2);
printf("NAME FOUND: %s",foundPerson->name);
break;
default:
break;
}
}
}