Hello!

I have a problem. I can't use scanf() to set a value of the second member of the person struct. Why? I get an error that tells me "bad access".

int main(int argc, const char * argv[])
{
    
    struct Adress{
        char *street;
        char *postal;
        int zip;
    };

    struct Person{
        char *firstName;
        char *lastName;
        struct Adress home;
    };
    
    struct Person newPerson;
    
    printf("Enter your first name and last name: \n");
    scanf("%s %s", newPerson.firstName, newPerson.lastName);
    printf("Hello %s %s! \n", newPerson.firstName, newPerson.lastName);
    
}

Dani AI

Generated

The crash happens because firstName and lastName in ’s struct are uninitialized pointers. Writing into them with scanf stores characters at an indeterminate address (undefined behavior). As noted, giving the struct fixed-size arrays places storage inside the struct; as showed, allocating memory before writing also works. Both approaches are valid — pick the one that fits your lifetime and size needs.

A few practical rules: never call scanf("%s", ...) into an unknown buffer. Either (a) use fixed arrays and always supply a width (so you cannot overflow), or (b) read into a temporary buffer (safe size), then allocate exactly what you need and copy. Prefer fgets (or POSIX getline) to read a whole line when names may contain spaces, then parse that line. Always check malloc results and free what you allocate. Initialize pointer members (for example set them to NULL) so errors are easier to spot.

Example pattern (safe, different from the posted snippets):

char line[256], tmp1[128], tmp2[128];
if (fgets(line, sizeof line, stdin) && sscanf(line, "%127s %127s", tmp1, tmp2) == 2) {
    newPerson.firstName = malloc(strlen(tmp1) + 1);
    newPerson.lastName  = malloc(strlen(tmp2) + 1);
    if (newPerson.firstName && newPerson.lastName) {
        strcpy(newPerson.firstName, tmp1);
        strcpy(newPerson.lastName, tmp2);
    }
}

Final notes: guard every buffer with sizes, prefer line-based input for text fields, and free allocated memory when done to avoid leaks.

Recommended Answers

All 4 Replies

You only have pointers. The memory space isn't allocated yet. If you were using arrays, that'd be different.

struct Adress{
        char street[32];
        char postal[32];
        int zip;
    };

    struct Person{
        char firstName[32];
        char lastName[32];
        struct Adress home;
    };

EDIT:
This might be helpful.

[edit]what ^^^ said.

Uninitialized pointers are not pointers to infinite memory. You have to allocate memory before trying to write to it:

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

int main(void)
{
    struct Person
    {
        char *firstName;
        char *lastName;
    };

    struct Person newPerson;

    newPerson.firstName = malloc(50);
    newPerson.lastName = malloc(50);

    printf("Enter your first name and last name: \n");
    scanf("%49s %49s", newPerson.firstName, newPerson.lastName);
    printf("Hello %s %s! \n", newPerson.firstName, newPerson.lastName);
    
    free(newPerson.firstName);
    free(newPerson.lastName);

    return 0;
}

It's also best to check and see if malloc() returns a null pointer, but I won't complicate the example with that yet.

commented: I was about to post something on malloc, but decided the poster wouldn't get it. Rock on, deceptikon. +4

Tank you! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.