Handout On Structures
Handout On Structures
(For example: You want to store information about a person: his/her name, citizenship
number and salary. You can create different variables name, citNo and salary to store these
information separately.
What if you need to store information of more than one person? Now, you need to create
different variables for each set of information per person: name1, citNo1, salary1, name2,
citNo2, salary2 etc.
A better approach would be to have a collection of all related information under a single
name Person structure, and use it for every person.
Defining a structure
Keyword struct is used for creating a structure.
Syntax of structure
struct structure_name
{
data_type member1;
data_type member2;
data_type member3;
};
Here is an example:
struct Person
{
char name[50];
int citNo;
float salary;
};
This definition above creates the derived data type struct Person.
Creating structure variable
When a structure is defined, it creates a user-defined type. However, NO storage or
memory is allocated. Therefore we need to create a structure variable to access the
members of the structure.
4
Variables declared within the braces of the structure definition are the
structure’s members.
Members of the same structure type must have unique names.
Method 1
struct Person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct Person person1;
return 0;
}
Method 2
struct Person
{
char name[50];
int citNo;
float salary;
} person1;
Suppose, you want to access salary of an individual, here’s how you can do it:
person1.salary
Example:
4
//Write C program that defines a structure called Person which contains 3 members: name,
city number and age. The program should accept the data for one person and display it.
#include <stdio.h>
struct Person
{
char name[50];
int citNo;
float salary;
} person1; //instantiating the structure person by creating a variable 'person1' to access all
members
int main()
{
//Accessing the members of the structure
printf("Enter information:\n");
printf("Enter name: ");
scanf("%s",&person1.name);
printf("\nDisplaying Information:\n");
printf("Name is : %s\n",person1.name);
printf("City Number is: %d\n",person1.citNo);
printf("Salary is: %.1f\n", person1.salary);
return 0;
}
//Write C program that defines a structure called Person which contains 3 members: name,
city number and age. The program should accept the data for 5 persons and display it.
#include <stdio.h>
struct Person
{
char name[50];
int citNo;
float salary;
} person1; //instantiating the structure person by creating a variable 'person1' to access all
members
int main()
{
int x;
4
//Accessing the members of the structure using a loop
for (x=0; x <5; x++)
{
printf("Enter information:\n");
printf("Enter name: ");
scanf("%s",&person1.name);
printf("\nDisplaying Information:\n");
printf("Name is : %s\n",person1.name);
printf("City Number is: %d\n",person1.citNo);
printf("Salary is: %.1f\n", person1.salary);
}
return 0;
}
1. Write C program that defines a structure called student which contains 3 members:
name, age and marks. The pass mark is 60. The program should print ‘Pass’ or ‘Fail’
accordingly.
NB:
- Person1.salary = 4500;
- Scanf(“%f”, &person1.salary);
struct Person
{
char name[50];
int citNo;
float salary;
} person1 [10]; // [10] to indicate the size of the array