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

Structuresc

Structures allow the grouping of related data under a single name. They are composed of variables called fields or components. Structures are commonly used to define records stored in files. An example structure defines a student record with fields for name, course, and year. Structures can be accessed using the dot operator to reference individual fields. Arrays of structures can also be defined to store multiple records. Individual records in the array are accessed using indexes and the dot operator.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Structuresc

Structures allow the grouping of related data under a single name. They are composed of variables called fields or components. Structures are commonly used to define records stored in files. An example structure defines a student record with fields for name, course, and year. Structures can be accessed using the dot operator to reference individual fields. Arrays of structures can also be defined to store multiple records. Individual records in the array are accessed using indexes and the dot operator.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Structure definition

Structure is a collection of related variables,

sometimes referred to as aggregates, under one name.

These variables are also called fields or


components

Structures are commonly used to define records


to be stored in files. Structure definition syntax struct <structure_name> { variable1 definition; variable2 definition; . . . variablen definition; } Example: struct student_record { char name[15]; char course[6]; int year; }

struct student_record student;

ACCESSING MEMBERS in STRUCTURES To input values: cin>>student.name; cin>>student.course; To display values: cout<<student.name; cout<<student.course; ARRAY of STRUCTURES

Define the structure Define an array variable of the structure


Example: struct student_record student[40]; ACCESSING MEMBERS in ARRAY STRUCTURES cout<<student[index].name; cout<<student[index].course;

#include <iostream.h> struct student_record { char lastname[15]; char firstname[10]; char course[8]; int year; }; void displayRec(struct student_record student[], int size) { int i; cout<<"LastName"; setspace(15); cout<<"Firstname"; setspace(15); cout<<"Course "; setspace(15); cout<<"Year"<<endl; for(i=0; i<size;i++) { cout<<student[i].lastname; setspace(15); cout<<student[i].firstname; setspace(15); cout<<student[i].course; setspace(15); cout<<student[i].year<<"\n"; }

int main() { struct student_record classList[40]; int classsize=0; char ans; do{ cout<<"Input student name : "<<endl; cout<<"lastname : "; cin>>classList[classsize].lastname; cout<<"firstname : "; cin>>classList[classsize].firstname; cout<<"Course :"; cin>>classList[classsize].course; cout<<"Year :"; cin>>classList[classsize].year; classsize++; cout<<"Do you want to input another record? (Y/N) "; cin>>ans; }while((ans=='Y')|| (ans=='y')); displayRec(classList,classsize); } return 0;

Discuss the advantages of creating a structure in c+ +. Expand and Explain briefly structure in c++.

You might also like