EDIT: SOLVED. THANKS!
I have a function that returns a pointer to a struct I built. I have a problem that my code runs in debugging mode but crashes in normal mode, It crashes only when I'm trying to add kids to the person- starts at: if (kidsNumber>0){ I'm pretty sure it's initializing issue but I can't find where and I'm stuck. I tried to put flags and breakpoint and nothing helped.. Thank you!
Person* CreatePerson(){
Person* newPerson;
newPerson=(Person*)malloc(sizeof(newPerson));
if (newPerson==NULL){
return NULL;
}
InitPersonValues(newPerson);
char tempName[MAX_NAME];
int id;
int kidsNumber;
printf("Name:\n");
scanf("%s",tempName);
newPerson->name=(char*)malloc(1 + strlen(tempName));
if (newPerson->name == NULL){
return NULL;
}
strcpy(newPerson->name,tempName);
printf("ID:\n");
scanf("%d", &id);
newPerson->id=id;
printf("Num of kids:\n");
scanf("%d",&kidsNumber);
newPerson->numOfKids=kidsNumber;
if (kidsNumber>0){
newPerson->kids=malloc(kidsNumber*sizeof(char*));
if (newPerson->kids==NULL){
return NULL;
}
int i=0;
for (i=0;i<kidsNumber;i++){
strcpy(tempName,"");
printf("Kid #%d name:\n",i+1);
scanf("%s",tempName);
newPerson->kids[i]=malloc(strlen(tempName)+1);
if (newPerson->kids[i]==NULL){
return NULL;
}
// printf("%s\n",tempName);
strcpy(newPerson->kids[i],tempName);
printf("%s\n",newPerson->kids[i]);
}
}
return newPerson;
}