0% found this document useful (0 votes)
3 views

Structure

Structures (structs) in C are used to group related variables of different data types into a single entity. They can be declared with a specific syntax and can be initialized using assignment or initializer lists. Accessing and manipulating structure members is done using the dot operator or the pointer operator, and structures can also be used in arrays.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Structure

Structures (structs) in C are used to group related variables of different data types into a single entity. They can be declared with a specific syntax and can be initialized using assignment or initializer lists. Accessing and manipulating structure members is done using the dot operator or the pointer operator, and structures can also be used in arrays.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

STRUCTURE

Structures (also called structs) are a way to group several related


variables into one place. Each variable in the structure is known as a
member of the structure.
A structure can contain many different data types (int, float, char,
etc.).

Structure Declaration
Syntax
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};

1. Structure Variable Declaration with Structure

struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;

Example

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;
2. Structure Variable Declaration after Structure

// structure declared beforehand

struct structure_name variable1, variable2, .......;

Example
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;

The variables e1 and e2 can be used to access the values stored in


the structure. Here, e1 and e2 can be treated in the same way as the
objects in C++ and Java.

Create a struct variable with the name "s1":

struct myStructure {
int myNum;
char myLetter;
};

int main() {
struct myStructure s1;
return 0;
}
E.g.
struct employee
{ int id;
char name[10];
float salary;
};

Memory allocation:
Access Structure Members:

We can access structure members by using the ( . ) dot operator.

Syntax

structure_name.member1;
strcuture_name.member2;

Another way is By -> (structure pointer operator)

Initialize Structure Members:

Structure members cannot be initialized with the declaration. For


example, the following C program fails in the compilation.

struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};

So, we initialize

1. Initialization using Assignment Operator


struct structure_name str;
str.member1 = value1;
str.member2 = value2;
str.member3 = value3;
.
.
.
2. Initialization using Initializer List
struct structure_name str = { value1, value2, value3 };
In this type of initialization, the values are assigned in sequential
order as they are declared in the structure template.
3. Initialization using Designated Initializer List
Designated Initialization allows structure members to be initialized
in any order.
struct structure_name str = { .member1 = value1, .member2 =
value2, .member3 = value3 };
The Designated Initialization is only supported in C but not in C++.

EXAMPLE:
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
float salary;
}e1,e2; //declaring e1 and e2 variables for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal"); //copying string into char array
e1.salary=56000;
//store second employee information
e2.id=102;
strcpy(e2.name, "James Bond");
e2.salary=126000;
//printing first employee information Output:
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name); employee 1 id : 101
printf( "employee 1 salary : %f\n", e1.salary); employee 1 name : Sonoo Jaiswal
//printing second employee information employee 1 salary : 56000.000000
printf( "employee 2 id : %d\n", e2.id); employee 2 id : 102
printf( "employee 2 name : %s\n", e2.name); employee 2 name : James Bond
employee 2 salary :
printf( "employee 2 salary : %f\n", e2.salary);
126000.000000
return 0;
}
Array of Structures

Suppose, you have a array, e.g.-

#include<stdio.h>
struct student
{
char name[20];
int id;
float marks;
};
void main()
{
struct student s1,s2,s3;
int dummy;
printf("Enter the name, id, and marks of student 1 ");
scanf("%s %d %f",s1.name,&s1.id,&s1.marks);
scanf("%c",&dummy);
printf("Enter the name, id, and marks of student 2 ");
scanf("%s %d %f",s2.name,&s2.id,&s2.marks);
scanf("%c",&dummy);
printf("Enter the name, id, and marks of student 3 ");
scanf("%s %d %f",s3.name,&s3.id,&s3.marks);
scanf("%c",&dummy);
printf("Printing the details....\n");
printf("%s %d %f\n",s1.name,s1.id,s1.marks);
printf("%s %d %f\n",s2.name,s2.id,s2.marks);
printf("%s %d %f\n",s3.name,s3.id,s3.marks);
}
Output
Enter the name, id, and marks of student 1 James 90 90
Enter the name, id, and marks of student 2 Adoms 90 90
Enter the name, id, and marks of student 3 Nick 90 90

Printing the details....


James 90 90.000000
Adoms 90 90.000000
Nick 90 90.000000
So, we can write
Output:
#include<stdio.h> Enter Records of 5 students
#include <string.h> Enter Rollno:1
struct student{ Enter Name:Sonoo
int rollno; Enter Rollno:2
char name[10]; Enter Name:Ratan
}; Enter Rollno:3
int main(){ Enter Name:Vimal
int i; Enter Rollno:4
struct student st[5]; Enter Name:James
printf("Enter Records of 5 students"); Enter Rollno:5
for(i=0;i<5;i++){ Enter Name:Sarfraz
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno); Student Information List:
printf("\nEnter Name:"); Rollno:1, Name:Sonoo
scanf("%s",&st[i].name); Rollno:2, Name:Ratan
} Rollno:3, Name:Vimal
printf("\nStudent Information List:"); Rollno:4, Name:James
for(i=0;i<5;i++){ Rollno:5, Name:Sarfraz
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}

You might also like