Struct keyword
Struct Keyword A struct is a data type that allows you to define custom data types with specific fields and data types. These fields can be named and ha...
Struct Keyword A struct is a data type that allows you to define custom data types with specific fields and data types. These fields can be named and ha...
Struct Keyword
A struct is a data type that allows you to define custom data types with specific fields and data types. These fields can be named and have associated values.
Example:
c
struct student {
char name[50];
int age;
float marks;
};
struct student student1 = {"John Doe", 18, 90.5};
Key Concepts:
Fields: Each field in a struct has a name and a corresponding data type.
Values: Each field holds a specific value associated with the field.
Declaration: A struct is declared using the struct keyword followed by the name of the struct and the fields within curly braces.
Access: Fields can be accessed using the dot operator (e.g., student.name).
Struct Declaration: A struct can be declared inside another struct, creating nested data types.
Benefits of Using Structs:
Data Organization: Structs help organize data by grouping related fields together.
Code Reusability: You can define structs and reuse them in different parts of your code.
Data Encapsulation: Structs can hide internal details and expose only necessary information through getters and setters.
Additional Notes:
Structs can be used to define complex data types with multiple dimensions.
They are commonly used in C, C++, and other programming languages.
Structs can be initialized using parentheses after the struct name, followed by the field values separated by commas