A structure (or struct) is a user-defined data type that allows you to combine different data types into a single unit. Structures are used to represent a record or a collection of related data items.
The struct keyword is used to define a structure. Here's the general syntax:
struct structure_name {
data_type member1;
data_type member2;
...
data_type memberN;
};
Let's say we want to create a structure to represent a student with the following attributes:
Name, Age, GPA
Here's how we can declare this structure in C:
#include
#include
// Declaration of the structure
struct Student {
char name[50];
int age;
float gpa;
};
int main() {
// Declaration of a structure variable
struct Student student1;
// Assigning values to the structure members
strcpy(student1.name, "John Doe");
student1.age = 20;
student1.gpa = 3.8;
// Printing the structure members
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("GPA: %.2f\n", student1.gpa);
return 0;
}
Structure Declaration:
struct Student {
char name[50];
int age;
float gpa;
};
This declares a new structure type named Student with three members: name (a character array of size 50), age (an integer), and gpa (a float).
Structure Variable Declaration:
struct Student student1;
This creates a variable student1 of type struct Student.
Assigning Values:
strcpy(student1.name, "John Doe");
student1.age = 20;
student1.gpa = 3.8;The strcpy function is used to copy the string "John Doe" into the name member of student1. The age and gpa members are assigned values directly.
Accessing Structure Members:
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("GPA: %.2f\n", student1.gpa);The members of the structure are accessed using the dot (.) operator.
typedef keyword
In C programming, typedef is a keyword used to create an alias or a new name for an existing data type. This can simplify code and improve readability, especially when dealing with complex data types such as structures, pointers, and arrays.
The general syntax for typedef is:
typedef existing_type new_type_name;
#include
typedef unsigned long ulong;
int main() {
ulong a = 1000;
printf("%lu\n", a);
return 0;
}
In this example, unsigned long is given a new name ulong. Now, you can use ulong as an alias for unsigned long.
Using typedef with structures is very common and useful. Here's an example:
Without typedef:
#include
#include
struct Student {
char name[50];
int age;
float gpa;
};
int main() {
struct Student student1;
strcpy(student1.name, "John Doe");
student1.age = 20;
student1.gpa = 3.8;
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("GPA: %.2f\n", student1.gpa);
return 0;
}
With typedef:
#include
#include
typedef struct {
char name[50];
int age;
float gpa;
} Student;
int main() {
Student student1;
strcpy(student1.name, "John Doe");
student1.age = 20;
student1.gpa = 3.8;
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("GPA: %.2f\n", student1.gpa);
return 0;
}
Without typedef:
struct Student {
char name[50];
int age;
float gpa;
};
You have to use the struct keyword every time you declare a variable of this type:
struct Student student1;
With typedef:
typedef struct {
char name[50];
int age;
float gpa;
} Student;
The typedef keyword creates an alias Student for the structure type. Now you can declare variables without using the struct keyword:
Student student1;
typedef