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*));
        }
Martin Broadhurst
  • 8,983
  • 2
  • 27
  • 34
CS student
  • 33
  • 1
  • 4

1 Answers1

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));

George
  • 2,092
  • 1
  • 11
  • 26
DevX
  • 298
  • 4
  • 15