| by Arround The Web | No comments

How to Initialize a Struct in Accordance with C Programming

Structures are one of the most popular and effective components of the C programming language. Structs are essentially user-defined data types which allow you to create complex data structures with multiple values stored in each element. The appropriate initialization of structs is one of the most crucial aspects in dealing with them.

In this article, we’ll go through various C programming-compliant initialization methods for structs.

Initialize Struct in C Language

There are three methods to initialize structs in C languages, which are as follows:

Method 1: Initialization at Declaration

The first way to initialize a struct is to declare and allocate memory all at once. This means declaring a struct, allocating memory for it, and then setting all its fields to the desired values. This can be done as per in the given example.

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

int main(void) {

    typedef struct car{
    int number;
    int Price;
};

    struct car c1={3793,762644};
    printf("Number: %d\nPrice: %d",c1.number, c1.Price);

    return 0;
}

 

In this code, we are declaring and initializing a struct inside the main() function, and then print it.

Output

Method 2: Using Individual Assignments

Another way to initialize struct members is to first define a variable, then assign a value to each member individually. Keep in mind that since char arrays cannot be allocated with text, they must be explicitly copied using separate operations like memcpy(). Always keep in mind that the array’s length cannot be smaller than the string it is storing.

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

typedef struct Book{
    char Name[40];
    int price;
} Book;

int main(void) {
    Book b1;
    memcpy(&b1.Name, "Throne of Glass\0", 40);
    b1.price = 2700;
    printf("Name: %s\nPrice: %d",
           b1.Name, b1.price);
    return 0;
}

 

In this code, struct is declared outside the main function, and it is then initialized the main() function and then printed.

Output

Method 3: Dot Operator

The dot(.) operator can be used to populate structure variables during compilation. Follow the code given below:

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

typedef struct car{
    int number;
    int Price;
};

int main(void) {
    struct car c1;
    c1.number=2754824;
    c1.Price=4000000;
    printf("Number: %d\nPrice: %d",c1.number, c1.Price);
    return 0;
}

 

In this above-mentioned code, struct is declared outside the main() function, initialized inside the main function by the use of dot(.) operator and then printed by printf statement.

Output

Conclusion

According to C programming, there are three possible ways to initialize a struct. You can use any of these three methods according to your preference. Either it can be initialized and declared at once, or separately, or it can be initialized using dot(.) operator. In C programming, a struct can be initialized using any of these techniques.

Share Button

Source: linuxhint.com

Leave a Reply