Home > Net >  How to initialized (or assigned) a value of an member of a structure (abstract data type) with parti
How to initialized (or assigned) a value of an member of a structure (abstract data type) with parti

Time:11-16

Here's the structure that I tried to make:

struct {
    char *name;
    int age;
    bool married;
} person;

How to initialize the married with False value so that every people that declared is not married in the first place until it is assigned otherwise?

CodePudding user response:

There is no default value for dynamically allocated memory in C, and no built-in concept of constructor, so you need to implement setting the default values yourself. You can create a "constructor" function that does the allocation and initialisation, e.g.,

struct person *new_person(const char *name, int age) {
    struct person *p = malloc(sizeof(*p));
    if (p) {
        p->name = strdup(name); // (subject to availability of `strdup`)
        p->age = age;
        p->married = false;
    }
    return p;
}

Of course in this particular case the default value is zero, so you could also just zero the memory, but with the constructor approach you can add other error checking, etc.

CodePudding user response:

Write a function as for example

void default_init( struct people *person )
{
    person->name = NULL;
    person->age = 0;
    person->married = false;
}

And after declaring an object of the structure type

struct people person;

call the function

default_init( &person );

Or for example

struct people *person = malloc( sizeof( *person ) );
default_init( person );

Of course you could initialize an object of the structure type in the point of its declaration like for example

struct people person = { .name = NULL, .age = 0, .married = false };

CodePudding user response:

If you dynamically allocate the memory with calloc the field married will be false.

I would also use a flexible array member in the struct to allow single allocation, reallocation and free of the data. Member age cant be negative so more suitable type is unsigned int

struct person{
    unsigned age;
    bool married;
    char name[];
};

struct person *newPerson(const char *name, unsigned age)
{
    struct person *pr = calloc(1, sizeof(*pr)   strlen(name)   1);
    if(pr)
    {
        strcpy(pr -> name, name);
        pr -> age = age;
    }
    return pr;
}

CodePudding user response:

 You could declare a constructor like this, since the struct type acts exactly like a class (the difference is that it is public).

struct Person {
    char *name;
    int age;
    bool married;

    Person(): married(false), age(0), name("") 
    {
    }
} person;

or this way

struct Person {
    char *name;
    int age;
    bool married;
}

Person::Person(): married(false), age(0), name("")
{
}

example:

Person p1;
std::cout<< p1.married << std::endl;

0

CodePudding user response:

An automatic default for non-static objects is not possible in C.

Recall there is no partial initialization in C: all or nothing.

Alternative's:

Only use static objects that rely on zero initialization

person p1; // All members filled with zero bits.

int foo() {
  static person p2;  // All members filled with zero bits.
  person p3; // No initialization.

{ 0 }

int foo() {
  person p4 = { 0 }; // First member 0, other members are 0.

{ .married = false }

int foo() {
  person p5 = { .married = false }; // .married member false, other members are 0
  person p6 = { .married = 0 }; // .married member false, other members are 0

Use another object

const person p_init1 = { .married = false }; // .married member false, other members are 0

int foo() {
  person p7 = p_init1

compound literal

int foo() {
  person p8 = (person){ .married = false };

Assigning

compound literal

int foo() {
  person p9;
  ...  
  p9 = (person){ .married = false }; // All members copied
  • Related