Structures, or structs, serve as a method for consolidating multiple related variables into a single entity. Each variable within the structure is referred to as a member of the structure.
In contrast to an array, a structure can accommodate various data types such as integers, floats, characters, and more.
To create a structure, you utilize the “struct” keyword and define each member within curly braces.
struct MyStructure { // Structure declaration int myNum; // Member (int variable) char myLetter; // Member (char variable) }; // End the structure with a semicolon |
To access the structure, you need to instantiate a variable of that structure type.
Inside the main() method, use the struct keyword followed by the structure name, and then declare the structure variable.
Instantiate a struct variable named “s1”.
struct myStructure { int myNum; char myLetter; }; int main() { |
To access members of a structure, employ the dot syntax ( . ):
// Create a structure called myStructure struct myStructure { int myNum; char myLetter; }; int main() { // Assign values to members of s1 // Print values return 0; |
Now, you can effortlessly generate multiple structure variables with distinct values, utilizing just one structure.
// Create different struct variables struct myStructure s1; struct myStructure s2; // Assign values to different struct variables s2.myNum = 20; |
Keep in mind that in C, strings are essentially arrays of characters, and regrettably, you cannot assign a value directly to an array in this manner.
struct myStructure { int myNum; char myLetter; char myString[30]; // String }; int main() { // Trying to assign a value to the string // Trying to print the value return 0; |
An error will be triggered.
|
Fortunately, there is a solution for this! You can utilize the strcpy() function to assign the value to s1.myString, as shown below:
struct myStructure { int myNum; char myLetter; char myString[30]; // String }; int main() { // Assign a value to the string using the strcpy function // Print the value return 0; |
Result:
|
You can also assign values to members of a structure variable during declaration, all in a single line.
Simply include the values in a comma-separated list within curly braces {}. It’s important to note that you don’t need to use the strcpy() function for string values when using this technique.
// Create a structure struct myStructure { int myNum; char myLetter; char myString[30]; }; int main() { // Print values return 0; |
Note that the sequence of inserted values must correspond to the order of variable types declared in the structure (13 for int, ‘B’ for char, etc.). |
In the following example, the values of s1 are copied into s2:
struct myStructure s1 = {13, ‘B’, “Some text”}; struct myStructure s2; s2 = s1; |
To change or modify a value, you can utilize the dot syntax (.).
Additionally, when modifying a string value, the strcpy() function proves useful once more:
struct myStructure { int myNum; char myLetter; char myString[30]; }; int main() { // Modify values // Print values return 0; |
Adjusting values becomes particularly useful when duplicating structure values.
// Create a structure variable and assign values to it struct myStructure s1 = {13, ‘B’, “Some text”}; // Create another structure variable // Copy s1 values to s2 // Change s2 values // Print values |
Ok, so, how are structures useful?Consider a scenario where you need to develop a program to store various details about cars, like their brand, model, and year. The beauty of structures lies in the ability to create a unified “Car template” that can be applied to every car you define. Below is a real-life example to illustrate this concept. |
Utilize a structure to store diverse details about cars.
struct Car { char brand[50]; char model[50]; int year; }; int main() { printf(“%s %s %d\n”, car1.brand, car1.model, car1.year); return 0; |