In C programming, a struct (or structure) is a collection of variables (can be of different types) under a single name.
Define Structures
Before you can create structure variables, you need to define its data type. To define a struct, the struct
keyword is used.
Syntax of struct
struct structureName {
dataType member1;
dataType member2;
...
};
For example,
struct Person {
char name[50];
int citNo;
float salary;
};
Here, a derived type struct Person
is defined. Now, you can create variables of this type.
Create struct Variables
When a struct
type is declared, no storage or memory is allocated. To allocate memory of a given structure type and work with it, we need to create variables.
Here's how we create structure variables:
struct Person {
// code
};
int main() {
struct Person person1, person2, p[20];
return 0;
}
Another way of creating a struct
variable is:
struct Person {
// code
} person1, person2, p[20];
In both cases,
- person1 and person2 are
struct Person
variables - p[] is a
struct Person
array of size 20.
Access Members of a Structure
There are two types of operators used for accessing members of a structure.
.
- Member operator->
- Structure pointer operator (will be discussed in the next tutorial)
Suppose, you want to access the salary of person2. Here's how you can do it.
person2.salary
Example 1: C structs
#include <stdio.h>
#include <string.h>
// create struct with person1 variable
struct Person {
char name[50];
int citNo;
float salary;
} person1;
int main() {
// assign value to name of person1
strcpy(person1.name, "George Orwell");
// assign values to other person1 variables
person1.citNo = 1984;
person1. salary = 2500;
// print struct variables
printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);
return 0;
}
Output
Name: George Orwell Citizenship No.: 1984 Salary: 2500.00
In this program, we have created a struct
named Person
. We have also created a variable of Person
named person1.
In main()
, we have assigned values to the variables defined in Person
for the person1 object.
strcpy(person1.name, "George Orwell");
person1.citNo = 1984;
person1. salary = 2500;
Notice that we have used strcpy()
function to assign the value to person1.name.
This is because name is a char
array (C-string) and we cannot use the assignment operator =
with it after we have declared the string.
Finally, we printed the data of person1.
Keyword typedef
We use the typedef
keyword to create an alias name for data types. It is commonly used with structures to simplify the syntax of declaring variables.
For example, let us look at the following code:
struct Distance{
int feet;
float inch;
};
int main() {
struct Distance d1, d2;
}
We can use typedef
to write an equivalent code with a simplified syntax:
typedef struct Distance {
int feet;
float inch;
} distances;
int main() {
distances d1, d2;
}
Example 2: C typedef
#include <stdio.h>
#include <string.h>
// struct with typedef person
typedef struct Person {
char name[50];
int citNo;
float salary;
} person;
int main() {
// create Person variable
person p1;
// assign value to name of p1
strcpy(p1.name, "George Orwell");
// assign values to other p1 variables
p1.citNo = 1984;
p1. salary = 2500;
// print struct variables
printf("Name: %s\n", p1.name);
printf("Citizenship No.: %d\n", p1.citNo);
printf("Salary: %.2f", p1.salary);
return 0;
}
Output
Name: George Orwell Citizenship No.: 1984 Salary: 2500.00
Here, we have used typedef
with the Person
structure to create an alias person
.
// struct with typedef person
typedef struct Person {
// code
} person;
Now, we can simply declare a Person
variable using the person
alias:
// equivalent to struct Person p1
person p1;
Nested Structures
You can create structures within a structure in C programming. For example,
struct complex {
int imag;
float real;
};
struct number {
struct complex comp;
int integers;
} num1, num2;
Suppose, you want to set imag of num2 variable to 11. Here's how you can do it:
num2.comp.imag = 11;
Example 3: C Nested Structures
#include <stdio.h>
struct complex {
int imag;
float real;
};
struct number {
struct complex comp;
int integer;
} num1;
int main() {
// initialize complex variables
num1.comp.imag = 11;
num1.comp.real = 5.25;
// initialize number variable
num1.integer = 6;
// print struct variables
printf("Imaginary Part: %d\n", num1.comp.imag);
printf("Real Part: %.2f\n", num1.comp.real);
printf("Integer: %d", num1.integer);
return 0;
}
Output
Imaginary Part: 11 Real Part: 5.25 Integer: 6
Why structs in C?
Suppose you want to store information about a person: his/her name, citizenship number, and salary. You can create different variables name, citNo and salary to store this information.
What if you need to store information of more than one person? Now, you need to create different variables for each information per person: name1, citNo1, salary1, name2, citNo2, salary2, etc.
A better approach would be to have a collection of all related information under a single name Person
structure and use it for every person.