Syntax
struct name/tag { //structure members } variables;
Example :
struct car { char name[100]; float price; } car1;
We can also declare many variables using comma (,) like below,
struct car { char name[100]; float price; } car1, car2, car3;
struct car { char name[100]; float price; }; //car1 name as "xyz" //price as 987432.50 struct car car1 ={"xyz", 987432.50};
When a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created. So, when we execute the following code it will show COMPILER ERROR.
struct Point { int x = 0; // COMPILER ERROR: cannot initialize members here int y = 0; // COMPILER ERROR: cannot initialize members here };
Example2 :
{ int x, y; }; int main() { // A valid initialization. member x gets value 0 and y // gets value 1. The order of declaration is followed. struct Point p1 = {0, 1}; }
There are many ways for initializing structure in c. Today I will show you every possible ways for c struct initialization. We have to use struct keyword to declare a structure. Let, we have a programmer struct like this
struct programmer { char name[100]; int roll; float marks; };
// Declare structure variable
struct programmer stu1;
// Initialize structure members
stu1.name = "John";
stu1.roll = 12;
stu1.marks = 79.5f;
We can access structure variable either with dot (.) or arrow (->) operator.
// Declare and initialize structure variable struct programmer stu1 = { "John", 12, 79.5f };
Invalid initialization:
// Declare and initialize structure variable struct programmer stu1 = { 12, "John", 79.5f };
This will throw compilation error. Because our initialization structure is mismatch with its declaration type. The values for the value initialized structure should match the order in which structure members are declared.
C Struct Initialization Inside Main
struct programmer { int mark1; int mark2; int mark3; }; void main() { struct programmer s1 = {89,54,65}; - - - - -- - - - - -- - - - - -- };
struct programmer { char name[20]; int roll; float marks; }std1 = { "Pritesh",67,78.3 };
struct programmer { char name[20]; int roll; float marks; } std1 = {"Pritesh",67,78.3}; std2 = {"Don",62,71.3};
Default initialization of a variable considered as good programming practice. We have to manually initialize all fields 0or NULL.
// Define macro for default structure initialization #define NEW_ programmer { "", 0, 0.0f } // Default initialization of structure variable struct programmer stu1 = NEW_ programmer;
Declaring & Initializing Structure in C Example:
#include <stdio.h> /*structure declaration*/ struct employee{ char name[30]; int empId; float salary; }; int main() { /*declare and initialization of structure variable*/ struct employee emp={"Mike",1120,76909.00f}; printf("n Name: %s" ,emp.name); printf("n Id: %d" ,emp.empId); printf("n Salary: %fn",emp.salary); return 0; }
After executing the above program, our output looks like
Name: Mike Id: 1120 Salary: 76909.000000
Today we learn how to declare a struct in c, initializing structure in c. I hope you guys understand everything which I have discussed earlier. So guys, that’s all for today. Later we will discuss another tutorial on c programming. Till then, take care. Happy Coding