# Explore hunderds of solved example topic wise.

## Structures in C

### What is Structure

The structure is the collection of different types of elements so it is called Heterogenous collection of data. It is used to create new user-defined data types.

For example, if you want to collect a person's data then a person can have the following data parameter.

```char name[20];
int age;
```
There is a way to combine the above field under a single name Person called a structure that makes it easier to handle.

### Creating a structure

```struct Person {
char name[20];
int age;
};
```
A shown in the above code snippet data fields name and age are grouped in a single block and named Person this can be done by using struct keyword in C.

Now Person is defined as user-defined data type and now you can create a variable of type Person.

### Creating object of structure

To create a variable from struct just define a variable with struct type as shown below...

`struct Person p1,p2;`
1. As shown above, p1 and p2 are two variables of type Person. Remember it is mandatory to use the struct keyword when you are creating a variable of a struct data type in C language. Even if you are working with C++ use of struct keyword in the above code snippet is not necessary.
2. As you can observe a person represents a real-world entity which have properties name and age, so it is better to say struct type variable an object rather than a variable.

### Memory of structure objects

struct define a data type that doesn't require any memory until an object of that type is not created.

An object of struct type receive the memory equal to the sum of memory of each member of the struct.

```struct Person {
char name[20];
int age;
};

struct Person p1,p2;
```

As per the above code snippet, two objects p1 and p2 are created of struct type Person.

As each char occupies 1 byte so the name[20] occupy 20 bytes in memory.

Each integer occupies 4 bytes so age occupies 4 bytes in memory.

The total memory required by each object of Person type is (20 + 4 = ) 24 bytes. So p1 and p2 occupy 24 bytes each in memory.

### Declaring structure objects

There are two ways to declare structure objects.

```struct Person {
char name[20];
int age;
};

struct Person p1,p2;
```
```struct Person {
char name[20];
int age;
}p1,p2;
```

The first code snippet shows first declare the struct data type then declare structure objects.

Second code snippet show to declare the struct objects along with the data type declaration.

Structure objects can be defined globally or locally both, but generally objects defined locally.

Structure declaration is generally defined globally. so it can be available to every function in your program.

Remember that every struct data type declaration must end with a semi-colon.

### Initializing structure objects

```struct Person {
char name[20];
int age;
};

struct Person p1 = {"Alex",22};

struct Person arr[] = {
{"Alex",22},
{"Black",25},
{"clark",24}
};
```

As shown in the above code snippet to initialize an object assigns the member value in the same order of structure separated by a comma.

To initialize an array of objects to an array multiple objects are written separated by a comma.

### Accessing structure members

To access the structure member from the object dot(.) operator is used. The following example demonstrates how to access structure members.

#### Structure Declaration

```struct Person {
char name[20];
int age;
};
```

#### Object Creation and initialization

```struct Person p = {"Alex",22};
```

#### Accessing structure members

```printf("Name = %s\n",p.name);
printf("Age = %d\n",p.age);
```

### Program to access structure members

```#include<stdio.h>
struct Person {
char name[20];
int age;
};
int main() {
//Object declaraton
struct Person p1,p2;

//Input in an object
printf("Enter name and age for person-1: ");
scanf("%s%d",p1.name,&p1.age);

printf("Enter name and age for person-2: ");
scanf("%s%d",p2.name,&p2.age);

printf("Person-1:\n---------------------\n");
printf("%s\t%d\n\n",p1.name,p1.age);

printf("Person-2:\n---------------------\n");
printf("%s\t%d\n\n",p2.name,p2.age);

return 0;
}
```

#### Output

```Enter name and age for person-1: Alex 22
Enter name and age for person-2: Clark 32
Person-1:
---------------------
Alex    22

Person-2:
---------------------
Clark   32
```

### Program to find the highest scorer.

#### Objective

Program to create a struct Student with the member roll no, name,m1,m2,m3, and per. where m1,m2,m2 are 3 subject marks and per holds the percentage of a student. Enter 3 subject marks and calculate the percentage and find the highest scorer. Print the name of the highest scorer or if both have the equal percentage print the output Both have an equal percentage.

#### Program

```#include<stdio.h>
struct student {
int rollno;
char name[20];
int m1,m2,m3;
float per;
};
int main() {
struct student s1,s2;

printf("Enter roll no, name and 3 subject marks for student-1: ");
scanf("%d%s%d%d%d",&s1.rollno,s1.name,&s1.m1,&s1.m2,&s1.m3);

printf("Enter roll no, name and 3 subject marks for student-2: ");
scanf("%d%s%d%d%d",&s2.rollno,s2.name,&s2.m1,&s2.m2,&s2.m3);

s1.per = (s1.m1 + s1.m2 + s1.m3) / 3.0f;
s2.per = (s2.m1 + s2.m2 + s2.m3) / 3.0f;

if(s1.per > s2.per) {
printf("%s score the highest per.",s1.name);
}else if(s2.per > s1.per) {
printf("%s score the more per.",s2.name);
}else {
printf("Both have equal per.");
}
return 0;
}```

#### Output

```Enter roll no, name and 3 subject marks for student-1: 101 Alex 33 55 44
Enter roll no, name and 3 subject marks for student-2: 102 Clark 55 77 66
Clark score the more per.
```

### Array of objects

An array of any data type is created if you need to store a list of the data type. similarly, the structure of the array is also allowed to maintain a list of objects.

#### Creating an array of objects

```struct student {
int rollno;
char name[20];
int m1,m2,m3;
float per;
};

struct student s[20];
```

### Program to find the highest scorer from an array of student objects.

#### Objective

Create a program to declare an array of student objects. student struct is the same as it is declared in the previous article. Store student data in the array and calculate the percentage of each student. Finally print the detail of the highest scorer.

#### Program

```#include
struct student {
int rollno;
char name[20];
int m1,m2,m3;
float per;
};
int main() {
struct student s[20];
int n,i,maxpos;
float maxper;

printf("Enter how many student, (max:20): ");
scanf("%d",&n);

//input data for array
printf("\nEnter %d student data...\n-----------------------\n",n);
for(i = 0; i < n; i++) {
printf("Enter roll no,name and 3 subject marks: ");
scanf("%d%s%d%d%d",&s[i].rollno,s[i].name,&s[i].m1,&s[i].m2,&s[i].m3);

s[i].per = (s[i].m1 + s[i].m2 + s[i].m3) / 3.0f;
}

//find the max percentage and position of max scorer in array
maxper = s[0].per; maxpos = 0;
for(i = 1; i < n; i++) {
if(s[i].per > maxper) {
maxper = s[i].per;
maxpos = i;
}
}

//print the details of maximum scorer
printf("\nDetail of maximum scorer...\n-------------------------------\n");
printf("Roll No: %d\n",s[maxpos].rollno);
printf("Name = %s\n",s[maxpos].name);
printf("Per = %.2f\n",s[maxpos].per);
return 0;
}
```

#### Output

```Enter how many student, (max:20): 3

Enter 3 student data...
-----------------------
Enter roll no,name and 3 subject marks: 101 Alex 33 55 77
Enter roll no,name and 3 subject marks: 102 Clark 45 86 35
Enter roll no,name and 3 subject marks: 103 Elle 54 78 35

Detail of maximum scorer...
-------------------------------
Roll No: 103
Name = Elle
Per = 55.67
```