Additional info: A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.
In C programming language, a structure is a user-defined data type that groups together variables of different data types under a single name. Structures are useful when we need to store and manipulate related data items as a single unit.
Here is an example of a structure defination in C:
structure defination
struct student {
char name[50];
int age;
float marks;
};
In the above example, we have defined a structure named student
that has three members - name
, age
, and marks
.
name
is a character array of size 50, age
is an integer, and marks
is a float.
structure declaration
We can declare variables of this structure type as follows:
struct student s1;
This creates a variable s1
of type student
.
access the members of a structure
We can access the members of a structure using the dot operator (.
) as follows:
strcpy(s1.name, "John");
s1.age = 20;
s1.marks = 85.5;
This sets the values of the members name
, age
, and marks
of the variable s1
.
We can also initialize a structure at the time of declaration as follows:
struct student s1 = {"John", 20, 85.5};
Benefit
- memory consumption
- No memory consumed for definition of the structure until declared.
Rule
- You have to prefix struct keyword while using structure variable
- structure variable consume 6 byte memory for storage.
We can pass a structure to a function by value or by reference.
- passed by value
- When passed by value, a copy of the structure is passed to the function.
- passed by reference
- When passed by reference, a pointer to the structure is passed to the function.
// passing by value
void display(struct student s) {
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
printf("Marks: %.2f\n", s.marks);
}
void main() {
struct student s1 = {"John", 20, 85.5};
display(s1);
}
In the above example, we have defined a function display
that takes a struct student
parameter s
. We have also defined a variable s1
of type struct student
and initialized it. We then call the function display
and pass s1
as a parameter. The function displays the values of the members of s1
.
Structure is similar to an array but the only difference is that array is collection of similar data type but structure can be collection of similar or different data type. Each variable declared inside structure is called member of structure
Structures in C++ can contain two types of members:
Variables: We can create a structure with variables of different data types in C++.
Functions: Normal C++ functions. Along with variables, we can also include functions inside a structure declaration.
Structure declaration
The ‘struct’ keyword is used to create a structure followed by the structure name and structure's member variables are declared within braces.
struct structureName{
member1;
member2;
member3;
.
.
.
memberN;
};
Accessing the structure members: Two ways
1.using the dot operator(.)
We have to create an object of structure to access its members. Object is a variable of type structure. Structure members are accessed using the dot operator(.) between structure's object and structure's member name.
Syntax for creating object
struct struct-name obj;
2.using the operator(->)
How to declare structure variables?
A structure variable(In two ways) can either be declared with structure declaration or as a separate declaration like basic data types.
// A variable declaration with structure declaration.
struct structureName{
member1;
member2;
member3;
.
.
.
memberN;
}structureVariable ;
// The variable structureVariable is declared with structure 'structureName'
// A variable declaration as separate declaration like basic data types
struct structureName{
member1;
member2;
member3;
.
.
.
memberN;
};
int main()
{
struct structureName structureVariable; // The variable structureVariable is declared like a normal variable
}
Note: In C++, the struct keyword is optional before in declaration of a variable. But In C, it is mandatory.
Passing and Return Structure to and from Function
Similar to variables of built-in types, you can also pass structure variables to a function. structure can pass into function as function argument and we can also return structure from function.
Structure can be passed to function through its object therefore passing structure to function or passing structure object to function is same thing because structure object represents the structure.
Like normal variable, structure variable(structure object) can be pass by value or by references / addresses.
passing structure by value
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void Display(struct Employee);
int main()
{
struct Employee Emp = {1,"Kumar",29,45000};
Display(Emp);
}
void Display(struct Employee E)
{
printf("\n\nEmployee Id : %d",E.Id);
printf("\nEmployee Name : %s",E.Name);
printf("\nEmployee Age : %d",E.Age);
printf("\nEmployee Salary : %ld",E.Salary);
}
Output :
Employee Id : 1
Employee Name : Kumar
Employee Age : 29
Employee Salary : 45000
passing structure by reference/address
struct Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void Display(struct Employee*);
int main()
{
struct Employee Emp = {1,"Kumar",29,45000};
Display(&Emp);
}
void Display(struct Employee *E)
{
printf("\n\nEmployee Id : %d",E->Id);
printf("\nEmployee Name : %s",E->Name);
printf("\nEmployee Age : %d",E->Age);
printf("\nEmployee Salary : %ld",E->Salary);
}
Output :
Employee Id : 1
Employee Name : Kumar
Employee Age : 29
Employee Salary : 45000