Introduction
In this lesson, we will delve into the realm of structures in C programming. You will learn how to declare and initialize structs, how to access their members, and the differences between structs and unions. Additionally, we'll explore how to use structs as function parameters, enhancing your ability to create organized and modular code. By the end of this lesson, you should be confident in using structures in your C programs.
Declaring a Struct in C
To declare a struct in C, the struct keyword is used, followed by the structure name and its members enclosed in curly braces. Here’s the syntax:
struct StructName {
DataType member1;
DataType member2;
// ... other members
};
Example of Declaring a Struct
Let's create a struct named Person that contains details about an individual:
#include <stdio.h>
struct Person {
char name[50];
int age;
float height;
};
In this example, we defined a struct that holds a name, age, and height of a person.
Initializing Struct Variables
Struct variables can be initialized at the time of declaration or assigned values later. Here's how you can initialize them during declaration:
struct Person person1 = {
"Alice",
30,
5.5
};
Example of Initialization
To further illustrate this, let's add a function to print the Person struct values:
void printPerson(struct Person p) {
printf("Name: %s\n", p.name);
printf("Age: %d\n", p.age);
printf("Height: %.2f\n", p.height);
}
int main() {
struct Person person1 = {"Alice", 30, 5.5};
printPerson(person1);
return 0;
}
Output:
Name: Alice
Age: 30
Height: 5.50
In this code, we have successfully declared, initialized, and accessed the struct members using a function.
Accessing Struct Members Using the Dot Operator
To access members of a struct, the dot operator (.) is utilized. This allows you to retrieve or modify struct member values directly:
struct Person person1; // Declare struct variable
// Set values using the dot operator
strcpy(person1.name, "Bob");
person1.age = 25;
person1.height = 6.1;
Differentiating between Structs and Unions
Structs and unions are both user-defined data types, but they serve different purposes. In a struct, each member has its own allocated memory, allowing for various data types to be stored simultaneously. In contrast, a union shares the same memory location, storing only one member at a time. This makes unions more memory efficient for scenarios where you only need one value out of many.
Example of a Union
Here’s how you would declare a union based on the Person struct:
union PersonData {
struct Person person;
char address[100];
};
As a result, you can either store a Person or an address but not both simultaneously.
Implementing Functions that Accept Struct Parameters
Passing structs to functions is essential for managing complex data. You can pass structs by value or by reference (using pointers). Here’s an example of passing by reference:
void updatePerson(struct Person *p) {
p->age += 1; // Increase age by 1
}
int main() {
struct Person person1 = {"Alice", 30, 5.5};
updatePerson(&person1);
printPerson(person1);
return 0;
}
Output:
Name: Alice
Age: 31
Height: 5.50
In this example, we passed a pointer to person1, allowing the updatePerson function to modify its age directly.
Summary / Key Takeaways
In this lesson on Declaring and Using Structs, you have learned:
- Struct Declaration: How to declare and define structures in C with various data types.
- Initialization: Methods for initializing struct variables upon declaration and afterward.
- Member Access: Using the dot operator to access and modify struct members.
- Structs vs. Unions: The key differences between structs and unions, including memory allocation.
- Function Parameters: How to create functions that accept struct parameters, enhancing code modularity.
By mastering structs and their usage in C, you pave the way for writing cleaner, more efficient, and organized programs. As you explore more advanced topics in C programming, these fundamental concepts will serve as a strong foundation.