V
Vandana
Hello All,
Can anyone please explain why I ma getting segmentation fault in the
following program?
struct employee {
char *name;
struct {
int apmt;
int zip;
} addr;
};
struct employee * populate_employee(char *name, int apt, int zip) {
struct employee *emp;
emp->name = name;
emp->addr.apmt = apt;
emp->addr.zip = zip;
return emp;
}
int main() {
struct employee *emp1;
printf("From the main\n");
emp1 = populate_employee("tom", 5, 90);
printf("address of emp1 is %u\n", emp1);
printf("Values from emp1 %s, %d %d\n", emp1->name, emp1->addr.apmt,
emp1->addr.zip);
return 0;
}
If I remove the printf "From the main" then the program seg faults,
otherwise, the program executes but ends with seg fault.
Thanks for your help!
Can anyone please explain why I ma getting segmentation fault in the
following program?
struct employee {
char *name;
struct {
int apmt;
int zip;
} addr;
};
struct employee * populate_employee(char *name, int apt, int zip) {
struct employee *emp;
emp->name = name;
emp->addr.apmt = apt;
emp->addr.zip = zip;
return emp;
}
int main() {
struct employee *emp1;
printf("From the main\n");
emp1 = populate_employee("tom", 5, 90);
printf("address of emp1 is %u\n", emp1);
printf("Values from emp1 %s, %d %d\n", emp1->name, emp1->addr.apmt,
emp1->addr.zip);
return 0;
}
If I remove the printf "From the main" then the program seg faults,
otherwise, the program executes but ends with seg fault.
Thanks for your help!