0% found this document useful (0 votes)
47 views8 pages

Structure in C

Uploaded by

yashkshatriya108
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)
47 views8 pages

Structure in C

Uploaded by

yashkshatriya108
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/ 8

Study Notes

Structure In
C Language
C Language
Structure In C

We have about various data types in C , imagine a condition where you are asked to create a
heterogeneous data type , means that there is a data type that can hold different kind of data
according to your need.

C provides a user defined data type called "Structure" which allow to hold different kind of data
together.

With the help of structures we can create complex user defined data types.

Suppose there is an university and you are asked to store the data of students like records of
student name, age, department, permanent address etc. So you will require a data type that can
hold these all.

In structure data is stored in form of records.

Defining a structure

Syntax :

struct structure_name
{
//member variable 1
//member variable 2
//member variable 3
...
}[structure_variables];

Here "struct" keyword is used to define structure. The member variable of structure can be of
primary data type or derived data type.

Note : Name of structure is not mandatory but good programming practices suggest to provide
name to the structure.
Structure variables are also optional and you can define them later also.
Semicolon at the end of the structure is mandatory.

Example :

struct StudentDetails
{
char name[25];

2
C Language
int age;
char branch[10];
// F for female and M for male
char gender
char address[50];
};

here name, age, branch, gender and address are called structure element or structure members.
StudentDetail will be of struct type.

Declaring the structure


This can be done in two way
1.Variables can be declared during the defining of structure.
2.Variables can be declared after defining at any later time that means defined separately.

1)Declaring variable with structure definition

struct StudentDetails
{
char name[25];
int age;
char branch[10];
// F for female and M for male
char gender
char address[50];
} S1, S2;

1)Declaring variable separately

struct StudentDetails
{
char name[25];
int age;
char branch[10];
// F for female and M for male
char gender
char address[50];

3
C Language
};

struct StudentDetails S1,S2;

Here in both cases S1 and S2 are the variables of structure StudentDetails.

Accessing Structure members


Now the next point is how to access the members of structure by variables. We use a (.) operator
to access the members of structure.

Code 01 : Accessing the members of structure

#include<stdio.h>
#include<string.h>

struct Student
{
char name[25];
int age;
char branch[10];
char gender;
};

int main()
{
struct Student s1;

printf("Enter the student name\n" );

scanf(" %s ", s1.name);

printf("Enter the student age\n" );

scanf(" %d ", s1.age);

printf("Enter the student branch\n" );

scanf(" %s ", s1.branch);

4
C Language

printf("Enter the student gender\n" );

scanf(" %c ", s1.gender);

//printing the entered information

printf("Information entered by you is mentioned below\n" );

printf("Name of Student : %s\n", s1.name);

printf("Age of Student : %d\n", s1.age);

printf("Branch of Student : %s\n", s1.branch);

printf("Gender of Student : %c\n", s1.gender);

return 0;

Output :

Enter the student name

Jay

Enter the student age

19

Enter the student branch

Mathematics

Enter the student gender

5
C Language

Information entered by you is mentioned below

Name of Student : Jay

Age of Student : 19

Branch of Student : Mathematics

Gender of Student : M

Array Of Structure
We can have array of structure where each element of array will be a structure variable.

#include<stdio.h>

struct Employee
{
char ename[10];
int sal;
};

struct Employee emp[3];


int i, j;
void detail()
{
for(i = 0; i < 3; i++)
{
printf("\nEnter %dst Employee record:\n", i+1);
printf("\nEmployee name:\t");
scanf("%s", emp[i].ename);
printf("\nEnter Salary:\t");
scanf("%d", &emp[i].sal);
}
printf("\nDisplaying Employee record:\n");
for(i = 0; i < 3; i++)
{

6
C Language
printf("\nEmployee name is %s", emp[i].ename);
printf("\nSlary is %d", emp[i].sal);
}
}
void main()
{
details();
}

Nesting of Structure
struct student
{
char[30] name;
int age;
//structure for address which has different parts
struct Address
{
char[50] locality;
char[50] city;
int pincode;
}addr;
};

Structure as function arguments

We can pass a structure in a function arguments like we pass any other variable or array.

#include<stdio.h>
struct Student
{
char name[10];
int roll;
};

void show(struct Student st);

void main()

7
C Language
{
struct Student std;
printf("\nEnter Student record:\n");
printf("\nStudent name:\t");
scanf("%s", std.name);
printf("\nEnter Student rollno.:\t");
scanf("%d", &std.roll);
show(std);
}

void show(struct Student st)


{
printf("\nstudent name is %s", st.name);
printf("\nroll is %d", st.roll);
}

You might also like