Handout-8 CSM-1101.doc - ALIGARH MUSLIM UNIVERSITY...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
ALIGARH MUSLIM UNIVERSITY Department of Computer Science Course: MCA CSM-1101: Problem Solving Using C++ Academic Session 2017-2018 Handout-8 Topic: Structures. Teacher : Dr. Rafiqul Zaman Khan, Professor +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Objectives: To learn how to declare and use structures. To learn how to pass structures as parameters to functions. To learn how to differentiate between arrays & structures What is structure ? Structures are collections of related variables. Structures may contain variables of many different data types- in contrast to arrays that contain only elements of the same data type; Structures are commonly used to define records to be stored in files. Pointers and structures facilitate the formation of more complex data structures such as linked lists, queues, stacks, and trees. How to declare and initialize structures ? The elements can be of any type including enumerated types, arrays and even other structures. e.g: struct user_record { int id_no; STRING name; STRING dept; int no_of_books; USER_STATUS status; }; This declares a structure with user_record as tag. Again, memory is not allocated until a variable is declared. A variable with the above structure can be declared as: struct user_record user; We can simplify the variable declaration above, if we use typedef in the declaration. The above can then be defined as: 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
typedef struct { int id_no; STRING name; STRING dept; int no_of_books; USER_STATUS status; } USER_RECORD; The variable declaration then becomes: USER_RECORD user; Once a variable is declared, we can access the individual elements (called fields) of the record using the member operator, also called the dot ( . ) operator. We can combine declarations and declare structure variables at the same time that the structure type is declared. For example: struct date { int day, month, year; } arrival_date, departure_date; or without a structure tag structure { int day, month, year; } arrival_date, departure-date; Structure Initialization : struct employee agent1={“Omer Ali”,96,3524}; For example, the following program reads and prints values for the structure variable user: For example, the following program reads and prints values for the structure variable user : #include <stdio.h> #include<iostream.h> #include <string.h> typedef char STRING[81]; 2
Image of page 2
typedef struct { int id_no; STRING name; STRING dept; int no_of_books; int status; } USER_RECORD; int main() { USER_RECORD user; user.no_of_books=7; cout<<"Enter user details:\n"; cout<<"ID_NO: "<<endl; cin>>user.id_no; cout<<"NAME : "<<endl; fflush(stdin); gets(user.name); cout<<"DEPARTMENT: "<<endl; gets(user.dept); cout<<"STATUS [0 for student, 1 for staff or 2 for faculty: ]"; cin>>(user.status); cout<<"\nThe details you entered are:\n"<<endl; cout<<"-----------------------------------------\n"; cout<<"ID_NO: "<<user.id_no<<endl; cout<<"NAME: "<<user.name<<endl; cout<<"DEPARTMENT: "<<user.dept<<endl; cout<<"STATUS: "<<user.status<<endl; cout<<"NO OF BOOKS : "<<user.no_of_books<<endl; return 0; } Sample Output: 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
/************************************************************* The following program shows the structure declaration, initialization, accessing members, and structure assignments.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern