0

I am currently trying to learn C and in order to do that I have tried to create very simple code that requests the first name of a person and puts it into a struct. It should then proceed to print the information using the "print_contact" function.

For some reason I keep getting a Bus error 10 and I don't understand why this happens.

typedef struct {

    char *first_name;
    char *last_name;

} contact;

void print_contact(contact *c) {
    printf("Name: %s %s\n", c->first_name, c->last_name);
}

contact *populate_contact() {

    contact *c;
    char *input;

    c = (contact *)malloc(sizeof(contact));

    printf(">>> Please Input The First Name: ");
    fgets(input, 100, stdin);
    c->first_name = strdup(input);
    c->last_name = "Doe";

}


int main() {


    contact m = *populate_contact();

    print_contact(&m);


    return 0;
}
mark
  • 153
  • 1
  • 10

0 Answers0