Вы находитесь на странице: 1из 19

COMPUTER

ASSIGNMENT
SUBMITTED BY-DILJOT KAUR
BSC.HONS PHYSICS

What are  Structures  in C?


Structure stores the different types of elements i.e
heterogeneous elements.
Structure is a group of variables of different data types
represented by a single name. Lets take an example to
understand the need of a structure in C programming.

Lets say we need to store the data of students like student


name, age, address, id etc. One way of doing this would
be creating a different variable for each attribute, however
when you need to store the data of multiple students then
in that case, you would need to create these several
variables again for each student. This is such a big
headache to store data in this way.

We can solve this problem easily by using structure. We


can create a structure that has members for name, id,
address and age and then we can create the variables of
this structure for each student.

How to create a structure in C Programming


We use struct keyword to create a structure in C. The
struct keyword is a short form of structured data type.

struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;...};

Here struct_name can be anything of your choice.


Members data type can be same or different. Once we
have declared the structure we can use the struct name as
a data type like int, float etc.

Declareing variables of a structure


struct  struct_name  var_name;
or

struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;

} var_name;

Accessing data members of a structure using


a struct variable
1. Array elements are accessed using the Subscript
variable , Similarly Structure members are
accessed using dot [.] operator.
2. (.) is called as “Structure member Operator”.
3. Use this Operator in between “Structure
name” & “member name”
var_name.member1_name;
var_name.member2_name;…
Assigning values to structure members.
There are three ways to do this.
1) Using Dot(.) operator

var_name.memeber_name = value;

2) All members assigned in one statement

struct struct_name var_name =


{value for memeber1, value for memeber2 …so on
for all the members}

3) Designated initializers there is another way to do the


same using designated initializers. This is useful when we
are doing assignment of only few members of the
structure. In the following example the structure variable
s2 has only one member assignment.

#include <stdio.h>
struct numbers
{
int num1, num2;
};
int main()
{
// Assignment using using designated
initialization
struct numbers s1 = {.num2 = 22, .num1 =
11};
struct numbers s2 = {.num2 = 30};
printf ("num1: %d, num2: %d\n", s1.num1,
s1.num2);
printf ("num1: %d", s2.num2);
return 0;
}

Example of Structure in C

#include <stdio.h>

/* Created a structure here. The name of the


structure is
* StudentData.
*/
struct StudentData{
char *stu_name;
int stu_id;
int stu_age;
};
int main()
{
/* student is the variable of structure
StudentData*/
struct StudentData student;

/*Assigning the values of each struct member


here*/
student.stu_name = "Steve";
student.stu_id = 1234;
student.stu_age = 30;

/* Displaying the values of struct members */


printf("Student Name is: %s",
student.stu_name);
printf("\nStudent Id is: %d", student.stu_id);
printf("\nStudent Age is: %d", student.stu_age);
return 0;
}

Array of Structure :
Structure is used to store the information of One
particular object but if we need to store such 100
objects then Array of Structure is used.
Example
#include <stdio.h>

struct Bookinfo
{
char[20] bname;
int pages;
int price;
}book[3];

int main(int argc, char *argv[])


{
int i;

for(i=0;i<3;i++)
{
printf("\nEnter the Name of Book : ");
gets(book[i].bname);
printf("\nEnter the Number of Pages : ");
scanf("%d",book[i].pages);
printf("\nEnter the Price of Book : ");
scanf("%f",book[i].price);
}

printf("\n--------- Book Details ------------ ");

for(i=0;i<3;i++)
{
printf("\nName of Book : %s",book[i].bname);
printf("\nNumber of Pages : %d",book[i].pages);
printf("\nPrice of Book : %f",book[i].price);
}

return 0;}

Eplanation

1. }Here Book structure is used to Store the


information of one Book.
2. In case if we need to store the Information of 100
books then Array of Structure is used.
3. b1[0] stores the Information of 1st Book , b1[1]
stores the information of 2nd Book and So on We
can store the information of 100 books.

Program.
#include <stdio.h>

struct Bookinfo
{
char[20] bname;
int pages;
int price;
}book[3];

int main(int argc, char *argv[])


{
int i;

for(i=0;i<3;i++)
{
printf("\nEnter the Name of Book : ");
gets(book[i].bname);
printf("\nEnter the Number of Pages : ");
scanf("%d",book[i].pages);
printf("\nEnter the Price of Book : ");
scanf("%f",book[i].price);
}

printf("\n--------- Book Details ------------ ");

for(i=0;i<3;i++)
{
printf("\nName of Book : %s",book[i].bname);
printf("\nNumber of Pages : %d",book[i].pages);
printf("\nPrice of Book : %f",book[i].price);n
}

return 0;
}
Accessing Element in Structure Array
1. Array of Structure can be accessed using dot[.]
operator.
2. Here Records of 3 Employee are Stored.
3. ‘for loop’ is used to Enter the Record of first
Employee.
4. Similarly ‘for Loop’ is used to Display Record.

Example
#include<stdio.h>
#include<conio.h>

struct Employee
{
int ssn;
char ename[20];
char dept[20];
}emp[3];

//---------------------------------------------
void main()
{
int i,sum;

//Enter the Employee Details


for(i=0;i<3;i++)
{
printf("nEnter the Employee Details : ");
scanf("%d %s %s",&emp[i].ssn,emp[i].ename,emp[i].dept);
}
//Print Employee Details
for(i=0;i<3;i++)
{
printf("nEmployee SSN : %d",emp[i].ssn);
printf("nEmployee Name : %d",emp[i].ename);
printf("nEmployee Dept : %d",emp[i].dept);
}
getch(); }
Passing Structure to Function .
A structure can be passed to any function from main function
or from any sub function.
Structure definition will be available within the function only.
It won’t be available to other functions unless it is passed to
those functions by value or by address(reference).
Else, we have to declare structure variable as global variable.
That means, structure variable should be declared outside the
main function. So, this structure will be visible to all the
functions in a C program.
PASSING STRUCTURE TO FUNCTION IN C:
It can be done in below 3 ways.
1.Passing structure to a function by value

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

struct student
{
int id;
char name[20];
float percentage;
};

void func(struct student record);

int main()
{
struct student record;

record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;

func(record);
return 0;
}

void func(struct student record)


{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}

2. Passing structure to a function by address(reference)


EXAMPLE PROGRAM – PASSING STRUCTURE TO FUNCTION IN C
BY ADDRESS:
In this program, the whole structure is passed to another
function by address. It means only the address of the structure
is passed to another function. The whole structure is not passed
to another function with all members and their values. So, this
structure can be accessed from called function by its address

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

struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student *record);

int main()
{
struct student record;

record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;

func(&record);
return 0;
}

void func(struct student *record)


{
printf(" Id is: %d \n", record->id);
printf(" Name is: %s \n", record->name);
printf(" Percentage is: %f \n", record->percentage);
}
3. EXAMPLE PROGRAM TO DECLARE A STRUCTURE VARIABLE AS
GLOBAL IN C:
Structure variables also can be declared as global variables as
we declare other variables in C. So, When a structure variable is
declared as global, then it is visible to all the functions in a
program. In this scenario, we don’t need to pass the structure
to any function separately.

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

struct student
{
int id;
char name[20];
float percentage;
};
struct student record; // Global declaration of structure

void structure_demo();

int main()
{
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;

structure_demo();
return 0;
}

void structure_demo()
{
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
}

Вам также может понравиться