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*));
        }
CS student
  • 33
  • 1
  • 1
  • 4
  • You'll need to show more of your code, as the problem isn't here. –  Jan 01 '18 at 21:29
  • Oh, and don't cast the return value of malloc: https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc. –  Jan 01 '18 at 21:30
  • You could start with `Person *person = malloc(sizeof(Person));` to actually *have* a struct. – Bo Persson Jan 01 '18 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? – George Jan 01 '18 at 21:45

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,101
  • 1
  • 13
  • 27
DevX
  • 308
  • 5
  • 16