Structures in C
Overview
The article covers creating and declaring structures, accessing members, and initializing values with practical syntax examples. It also touches upon handling strings within structures using character arrays and provides a real-world example of managing a contact list.
Create a structure
To create a structure in C, the struct keyword is used, followed by the tag name of the structure. Then the body of the structure is defined, in which the required data members (primitive or user-defined data types) are added.
Syntax:
In the above syntax, the data_members can be of any data type like int, char, double, array or even any other user-defined data type. The data_member_definition for the data types like character array, int, double is just a variable name like name, class, and roll_no. We have also declared a variable, i.e., student1 of the Student structure. Please note that it is not mandatory always to declare structure variables in this way. We will see other ways in the coming sections.
C Structure Declaration
If we have created a Student structure for storing students data with all the data members like student name, student class and student section, how can we use them? To use the properties of the created structure in C, we have to create structure variables. There are two ways to declare variables for structure in C language:
Method 1
Syntax:
In above example, Student structure is created and student1 variable is declared for it just after the structure definition.
Method 2:
As we create a structure in C, we have created a user-defined data type. So this data type can be treated as the primitive data type while declaring a variable for that structure. Syntax:
Which one to prefer?
If we declare the structure variables with the structure definition, they work as global variables. If we need global variables we can declare variables with the structure otherwise declaring it using the second approach is the best way as it is easy to maintain or initialize variables.
C Structure Definition
To define a structure in C, you use the struct keyword followed by the structure name. Inside the structure, you list the member variables (fields) along with their data types. Here's an example:
In this example, we define a structure named Student with three member variables: studentID (an integer), name (a character array), and grade (a floating-point number).
Structure Variable Declaration with Structure Template
Here's an example:
struct Student student1;
In the above code, we declare a structure variable student1 of type Student based on the structure template we defined earlier.
Structure Variable Declaration after Structure Template
You can also declare structure variables right after the structure definition. Here's how you can do it:
In this case, we define the structure Student and declare two structure variables, student1 and student2, immediately after the structure definition.
Now, you can use these structure variables to store and manipulate data related to students. For example:
This code sets values for the student1 structure variable and prints the student's information.
Access Structure Members
To access structure members and create multiple structure variables with different values using just one structure definition, you can follow these steps.
- First, define a structure,
- And then declare and initialize multiple structure variables based on that structure. Here's an example:
In this example, we define a structure Point with two integer members: x and y. Then, we declare and initialize three structure variables (p1, p2, and p3) using the same structure definition.
Initialize Structure Members
Initialization using Assignment Operator
You can initialize structure members individually using the assignment operator (=) after declaring the structure variable. Here's an example:
In this example, we declare a structure variable p1 of type Point and then initialize its x and y members individually using the assignment operator.
Initialization using Initializer List
You can also initialize structure members using an initializer list when declaring the structure variable. Here's an example:
In this case, we declare and initialize p1 in a single step, providing values for its x and y members within curly braces.
Initialization using Designated Initializer List
Designated initializers allow you to specify the member you want to initialize explicitly. This is especially useful when you want to initialize only specific members of a structure. Here's an example:
In this example, we use designated initializers (.x and .y) to specify which members we want to initialize. This method is particularly useful when you have structures with many members, and you only want to initialize a few of them.
What About Strings in Structures?
Strings can be stored in structures in C, but it's important to understand how to work with them, as C does not have a built-in string data type like some other languages. Instead, C typically uses character arrays to represent strings.
Storing Strings in Structures
To store strings in structures, you can use character arrays as members of the structure. Here's an example:
In this example, we define a structure Person with a name member as a character array and an age member as an integer. We use the strcpy function from the string.h library to copy the name "John Doe" into the name member of person1.
Simpler Syntax for Initializing Structures
You can use a simpler syntax to initialize structures at the time of declaration:
This initializes person1 with the name "Alice Smith" and age 25 directly during declaration.
Copying Structures
You can copy the contents of one structure to another by using the assignment operator =:
This copies the values of person1 (name and age) into person2.
Modifying Values in Structures
You can modify values in a structure just like any other variable:
Real-Life Example
A real-life example could be a program that manages a contact list using structures to store information about each contact, including their name and phone number:
In this example, we define a Contact structure to store contact information, and we use an array of structures to manage a list of contacts, including their names and phone numbers.
Conclusion
- The article expertly demystifies C structures, providing clear syntax and usage examples.
- It emphasizes flexibility in declaration and initialization, catering to varied programming needs.
- Handling strings within structures is simplified, showcasing practical character array applications.
- The inclusion of a real-life contact management example solidifies the learning experience.