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

Handout On Structures

Structures are collections of related variables under one name that can contain variables of different data types unlike arrays. Structures are commonly used to define records stored in files. A structure is defined using the struct keyword followed by the structure name and members. Structure variables are then declared to access members using the dot operator or arrow operator on a structure pointer. Examples demonstrate defining, declaring, and accessing structure members and arrays of structures.

Uploaded by

aldene Walker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Handout On Structures

Structures are collections of related variables under one name that can contain variables of different data types unlike arrays. Structures are commonly used to define records stored in files. A structure is defined using the struct keyword followed by the structure name and members. Structure variables are then declared to access members using the dot operator or arrow operator on a structure pointer. Examples demonstrate defining, declaring, and accessing structure members and arrays of structures.

Uploaded by

aldene Walker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Structures/ Records

Structures—sometimes referred to as aggregates—are collections of related variables under


one name. Structures may contain variables of many different data types—in contrast to
arrays, which contain only elements of the same data type. Structures are commonly used to
define records to be stored in files.
Structures are derived data types—they’re constructed using objects of other types.

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

How to Access members of a structure?


There are two types of operators used for accessing members of a structure.
Member operator(.)
Structure pointer operator(->)

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("Enter city number : ");


scanf("%d", &person1.citNo);

printf("Enter salary: ");


scanf("%f", &person1.salary);

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.

Example with loop

#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("Enter city number : ");


scanf("%d", &person1.citNo);

printf("Enter salary: ");


scanf("%f", &person1.salary);

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.

2. Write a structure to store the name, account number and balance of 10


customers and store their information. Print the names of all the customers
having balance less than $1000 and add $100 to the balance of all the
customers having more than $2000 and then print the incremented value of
their balance.

NB:

To assign values to data members in a record in C:

- Person1.salary = 4500;
- Scanf(“%f”, &person1.salary);

To declare an array of a particular record in C:

struct Person
{
char name[50];
int citNo;
float salary;
} person1 [10]; // [10] to indicate the size of the array

You might also like