v_head
v_head

Reputation: 805

How to initialize C structs with default values

I have this defined struct:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

typedef struct List {
    int size = 0;
    Node* head = NULL;
    Node* tai = NULL;
} List;

List* list1;

For the the node one it is Ok, but for the List one I have a declaration error in visual studio (2022), I am new to C, how to declare default values in C structs?

Upvotes: 4

Views: 4144

Answers (4)

Madagascar
Madagascar

Reputation: 7345

typedef struct List {
    int size;
    Node* head;
    Node* tail;
} List;

What you have defined here is a new data type, you haven't declared any variables of such a type. The name List is not a variable, it is the name of a structure type. The names size, head and tail are not variables, they're the identifiers for the members of this struct.

How to declare default values in C structs?

You can not define a type with default value for the members. Simply provide a definition for it with/after declaration:

List apple;
memset (&apple, 0x00, sizeof apple);

/* Or */

List apple = { .size = 0, .head = NULL, .tail = NULL };

/* Or */

List apple = { .size = 0, .head = 0, .tail = 0 };

/* Or */ 

List mango = { 0, NULL, NULL };

/* Or */

List banana = { 0, 0, 0};

/* Or */

List orange = { 0 };¹

[1] — §6.7.9 Initialization:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration

Upvotes: 0

Andreas Wenzel
Andreas Wenzel

Reputation: 24736

In C, whether an object is initialized or not depends on how you declare the object, for example whether you declare it as an object of static storage duration (which is initialized to zero unless you explicitly initialize it to something else) or an object of automatic storage duration (which is not initialized, unless you explicitly initialize it).

Therefore, it would not make sense to assign default values to the type definition, because even if the language allowed this, it would not guarantee that the object of that type will be initialized.

However, you can create your own function which initializes your struct to specific values:

void init_list( List *p )
{
    p->size = 0;
    p->head = NULL;
    p->tail = NULL;
}

Assuming that the object is declared inside a function (not at file scope), you can use the following code to declare and initialize the object to default values:

List list1;
init_list( &list1 );

If the object is declared at file scope, you can't call the function init_list at file scope, but you can call the function inside the function main, for example.

Alternatively, when you declare the object, you can also initialize the individual members:

List list1 = { 0, NULL, NULL };

This will also work at file scope.

Since everything is being initialized to zero, it is sufficient to write the following:

List list1 = { 0 };

In that case, all members that are not explicitly assigned a value will be initialized to zero.

Upvotes: 2

Ted Lyngmo
Ted Lyngmo

Reputation: 117298

In C you can't define a struct with default values for the members.

You can however create a global instance with the default values set that you then use for initialization. That's pretty common in the C world.

Example:

typedef struct List {
    int size;
    Node* head;
    Node* tail;
} List;

// The init-value to use
const List List_INIT = {.size = 0, .head = NULL, .tail = NULL};

int main() {
    List l = List_INIT; // using the init-value
}

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

In C opposite to C++ you may not initialize data members in structure declarations like you are doing

typedef struct List {
    int size = 0;
    Node* head = NULL;
    Node* tai = NULL;
} List;

Also it does not make sense to declare the global pointer list1.

List* list1;

What you need is to write

typedef struct List {
    int size;
    Node* head;
    Node* tail; // I think you mean `tail` instead of `tai`
} List;

int main( void )
{
    List list1 = { .size = 0, .head = NULL, .tail = NULL };
    //...;

Upvotes: 2

Related Questions