Home > Software design >  Allocate a new structure initializes it with the provided parameters
Allocate a new structure initializes it with the provided parameters

Time:01-12

I want to create a structure in which represents a people. So i started declare a structure People that contains 3 fields: - firstname : a pointer to a string which represents the first name - lastname : a pointer to a string that represents the name and age : an integer representing the age.

Now i have to write a function which allocates a new People structure and initializes it with copies of the provided parameters.

The problem i have is that the program always crashes. I dont understand well how can i fill the structure without crashing (thats an online compiler by my school). Can someone explain and correct me please ?

This is how its looks like :

Array.h :

#ifndef _PEOPLE_H_                                           
#define _PEOPLE_H_                                           
                                             
typedef struct {                                             
    char* firstname;                                             
    char* lastname;                                          
    int age;                                             
} People;                                            
                                             
People* createPeople(char* firstname, char*  lastname, int age);                                             
                                             
#endif                                            

Array.c :

#include "People.h"                                          
People* createPeople(char* firstname, char*  lastname, int age) {                                            
    People people1;                                          
                                             
    people1.firstname = firstname;                                           
    people1.lastname = lastname;                                             
    people1.age = age;                                           
}                                            

CodePudding user response:

You cannot return a pointer to a local variable as it is out of scope upon return of function. This means you either need to pass in an instance of Person, or as here, use malloc() to dynamically allocate an instance. I also called strdup() on the two strings, in this example it is not required, but the createPeople() function doesn't know when the objects they point to are out of scope. Minor issue, People is plural, but you only create a a Person.

#include <stdlib.h>
#include <string.h>

typedef struct {
    char* firstname;
    char* lastname;
    int age;
} Person;

Person* createPerson(char* firstname, char*  lastname, int age) {
    Person *p = malloc(sizeof(*p));
    if(!p) return NULL;
    p->firstname = strdup(firstname);
    p->lastname= strdup(lastname);
    p->age = age;
    return p;
}

int main() {
    Person *p = createPerson("Clark", "Kent", 42);
    free(p->firstname);
    free(p->lastname);
    free(p);
}

CodePudding user response:

You first have to include stdlib.h to be able to use malloc and free. Then you allocate enough memory to hold the struct People using malloc.

If the memory allocation fails you return NULL.

If the memory allocation goes well, then you assign each member of the struct value and return it.

#include "People.h"
#include <stdlib.h>
                                          
People* createPeople(char* firstname, char*  lastname, int age) {                                            
    People *people1 = malloc(sizeof(*people1));
    if (!people1)
         return (NULL);                             
    people1->firstname = firstname;                                           
    people1->lastname = lastname;                                             
    people1->age = age;

   return (people1);                                           
}
  • Related