KEMBAR78
Structures | PDF | Computer Data | Object Oriented Programming
0% found this document useful (0 votes)
4 views3 pages

Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Structures

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C Structure

Why use Structure?


In C, there are cases where we need to store multiple attributes of an entity. It is not necessary that an
entity has all the information of one type only. It can have different attributes of different data types. For
example, an entity Student may have its name (string), roll number (int), marks (float). To store such
type of information regarding an entity student, we have the following approaches:
 Construct individual arrays for storing names, roll numbers, and marks.
 Use a special data structure to store the collection of different data types.

What is Structure
Structure in c is a user-defined data type that enables us to store the collection of different data types.
Each element of a structure is called a member. Structures ca; simulate the use of classes and
templates as it can store various information.
The, struct keyword is used to define the structure. Let's see the syntax to define the structure in c.
Syntax:
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};

Example:
struct employee
{
int id;
char name[20];
float salary;
};
The following image shows the memory allocation of the structure employee that is defined in the
above example.
Declaring structure variable
We can declare a variable for the structure so that we can access the member of the structure easily.
There are two ways to declare structure variable:
1. By struct keyword within main() function
2. By declaring a variable at the time of defining the structure.

1. By struct keyword within main() function


Let's see the example to declare the structure variable by struct keyword. It should be declared
within the main function.
struct employee {
int id;
char name[50];
float salary;
};
Now write given code inside the main() function.
struct employee e1, e2;

2. By declaring a variable at the time of defining the structure.


Let's see another way to declare variable at the time of defining the structure.
struct employee {
int id;
char name[50];
float salary;
} e1, e2;

Structure Initialization
The initialization of a struct variable is done by placing the value of each element inside curly brackets.
Example
The following statement demonstrates the initialization of structure
struct employee e = {1002, “Santosh”, 50000};
Accessing the Structure Members
To access the members of a structure, first, you need to declare a structure variable and then use the
dot (.) operator along with the structure variable.

#include <stdio.h>
struct employee {
int id;
char name[50];
float salary;
};

int main(){
struct employee emp = {1002, "Santosh", 50000};

printf("Employee ID: %d", emp.id);


printf("Employee Name: %s", emp.name);
printf("Employee Salary: %d", emp.salary);
}
Copying Structures
The assignment (=) operator can be used to copy a structure directly. You can also use the
assignment operator (=) to assign the value of the member of one structure to another.
Example:
#include <stdio.h>
struct employee {
int id;
char name[50];
float salary;
};

int main(){
struct employee emp1 = {1002, "Santosh", 50000};
struct employee emp2;
emp1 = emp2;
printf("Employee ID: %d", emp2.id);
printf("Employee Name: %s", emp2.name);
printf("Employee Salary: %d", emp2.salary);
}
Example:
#include <stdio.h>
struct employee {
int id;
char name[50];
float salary;
};

int main(){
struct employee emp1 = {1002, "Santosh", 50000};
struct employee emp2;
emp1.id = emp2.id;
strcpy(emp2.name, emp1.name)
emp1.salary = emp2.salary;
printf("Employee ID: %d", emp2.id);
printf("Employee Name: %s", emp2.name);
printf("Employee Salary: %d", emp2.salary);
}

Structures as Function Arguments


You can pass a structure as a function argument in the same way as you pass any other variable or
pointer.

You might also like