0

I have a struct Person, with a name, id, and number of kids. I'm trying to create a dynamic array for the names, id and number of kids. Keep getting the error "uninitialized local variable 'name' used"

        Person *person;
        printf("Add a person to the game? (0|1)");
        scanf("%c",&dummy);
        scanf("%d",&input);
        while (input == 1)
        {

            person->name =(char*)malloc(strlen(arr));
            if (person->name == NULL)
                return NULL;
            person->id = (int*)malloc(ID*sizeof(int));
            if (person->id == NULL)
                return NULL;
            person->kids = (char*)malloc(kidNum * sizeof(char*));
        }
4
  • You'll need to show more of your code, as the problem isn't here. Commented Jan 1, 2018 at 21:29
  • Oh, and don't cast the return value of malloc: stackoverflow.com/questions/605845/…. Commented Jan 1, 2018 at 21:30
  • You could start with Person *person = malloc(sizeof(Person)); to actually have a struct. Commented Jan 1, 2018 at 21:33
  • Ignoring some of the errors in your loop, person->name won't point to an array of names, perhaps you should allocate an "array" of Person objects? Commented Jan 1, 2018 at 21:45

1 Answer 1

2

I am not C expert but seeing your code it seems like you are creating a pointer to a struct and then your pointer is not initialized to any thing. This may be the reason for your problem. I am not sure of the proper syntax but try this:

Person* person = malloc(sizeof(Person));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.