c-example-demonstrate-initilization-of-strucure
admin
Demonstrating the initialization of structures in C programming with sample code.
Are you looking to understand the initialization of structures in C programming? This article covers everything from basic concepts to implementation with a sample program. We will also discuss structure pointers and how to use them effectively. Let's dive in!
A structure in C is a user-defined data type that allows grouping different types of variables under one name. Structures are essential for creating more complex programs, as they enable the handling of related data efficiently.
Structure initialization involves assigning values to the members of a structure. This can be done at the time of declaration or later in the program. Here's how:
You can assign default values to structure members within the structure definition (if your compiler supports it).
struct student {
int sno; // Roll number
char sname[20]; // Name
float per; // Percentage
};
A structure pointer's declaration is similar to that of a structure variable. The *
symbol is used before the variable name to declare a pointer variable.
Below is an example program to illustrate how to initialize and use a structure in C:
#include <stdio.h>
// Define the structure
struct student {
int sno; // Student number
char sname[20]; // Name
float per; // Percentage
};
int main() {
// Initialize structure members
struct student s1 = {101, "Sumit", 70.9};
struct student s2 = {102, "Ravi", 85.5};
// Display structure members
printf("Student 1: %d %s %.2f\n", s1.sno, s1.sname, s1.per);
printf("Student 2: %d %s %.2f\n", s2.sno, s2.sname, s2.per);
return 0;
}
Student 1: 101 Sumit 70.90
Student 2: 102 Ravi 85.50
main()
function..
operator to access structure members for a variable.->
operator to access structure members via a pointer.To declare and use a pointer for a structure:
#include <stdio.h>
struct student {
int sno;
char sname[20];
float per;
};
int main() {
struct student s1 = {101, "Sumit", 70.9};
struct student *ptr = &s1; // Pointer to structure
// Access members using the pointer
printf("Student: %d %s %.2f\n", ptr->sno, ptr->sname, ptr->per);
return 0;
}
Consider a scenario where you want to define a car with specific attributes like name and price. Here's how you can initialize and use such a structure:
#include <stdio.h>
struct car {
char name[100];
float price;
};
int main() {
struct car car1 = {"Tesla Model S", 79999.99};
printf("Car: %s, Price: $%.2f\n", car1.name, car1.price);
return 0;
}
Car: Tesla Model S, Price: $79999.99
In this article, we explored how to initialize structures in C with examples and practical use cases. Structures are a powerful feature in C that helps organize data efficiently. Whether you’re handling student records or car details, knowing how to initialize and use structures is essential for any programmer.
If you found this guide helpful, check out our other articles on C programming for beginners, and start mastering the basics today!