Record - Struct

download Record - Struct

of 29

Transcript of Record - Struct

  • 7/28/2019 Record - Struct

    1/29

    Records (struct)

  • 7/28/2019 Record - Struct

    2/29

    2

    Objectives

    In this chapter, you will:

    Learn about records (struct)

    Examine various operations on a struct

    Explore ways to manipulate data using a

    struct

    Learn about the relationship between a struct

    and functions Discover how arrays are used in a struct

    Learn how to create an array ofstruct items

  • 7/28/2019 Record - Struct

    3/29

    Record/Structure

    Major limitation of arrays: every element of an

    array must be of the same type

    Sometimes it is necessary to organise related

    data items having different data types

    e.g. Student Record:

    Name string

    ID number integer CGPA float

    A unit of such data structure is called a

    "record"

    STUDENT RECORD

    Name Ahmad Al-Bab

    ID 1007

    CGPA 3.99

  • 7/28/2019 Record - Struct

    4/29

    4

    Records (structs)

    struct: collection of a fixed number of

    components (members), accessed by name

    Members may be of different types

    Syntax:

  • 7/28/2019 Record - Struct

    5/29

    5

    Records (structs) (continued)

    A struct is a definition, not a declaration

  • 7/28/2019 Record - Struct

    6/29

    6

    Records (structs) (continued)

  • 7/28/2019 Record - Struct

    7/297

    Accessing struct Members

    The syntax for accessing a structmember is:

    The dot (.) is an operator, called the memberaccess operator

  • 7/28/2019 Record - Struct

    8/29

    8

    Accessing struct Members

    (continued)

    To initialize the members ofnewStudent:newStudent.GPA = 0.0;

    newStudent.firstName = "John";

    newStudent.lastName = "Brown";

  • 7/28/2019 Record - Struct

    9/29

    9

    Accessing struct Members

    (continued) More examples:

    cin >> newStudent.firstName;cin >>newStudent.testScore >> newStudent.programmingScore;

    score = (newStudent.testScore +newStudent.programmingScore) / 2;

    if (score >= 90)newStudent.courseGrade = 'A';

    else if (score >= 80)newStudent.courseGrade = 'B';

    else if (score >= 70)newStudent.courseGrade = 'C';

    else if (score >= 60)newStudent.courseGrade = 'D';

    elsenewStudent.courseGrade = 'F';

  • 7/28/2019 Record - Struct

    10/29

    10

    Assignment

    Value of one struct variable can be

    assigned to anotherstruct variable of the

    same type using an assignment statement

    The statement:

    student = newStudent;

    copies the contents ofnewStudent into

    student

  • 7/28/2019 Record - Struct

    11/29

    11

    Assignment (continued)

    The assignment statement:

    student = newStudent;

    is equivalent to the following statements:

    student.firstName = newStudent.firstName;

    student.lastName = newStudent.lastName;

    student.courseGrade =newStudent.courseGrade;

    student.testScore = newStudent.testScore;

    student.programmingScore =newStudent.programmingScore;

    student.GPA = newStudent.GPA;

  • 7/28/2019 Record - Struct

    12/29

    12

    Comparison (Relational Operators)

    Compare struct variables member-wise

    No aggregate relational operations allowed

    To compare the values ofstudent andnewStudent:

  • 7/28/2019 Record - Struct

    13/29

    13

    Input/Output

  • 7/28/2019 Record - Struct

    14/29

    14

    struct Variables and Functions

    A struct variable can be passed as a

    parameter by value or by reference

    A function can return a value of type struct

  • 7/28/2019 Record - Struct

    15/29

    15

    Arrays in structs

  • 7/28/2019 Record - Struct

    16/29

    16

    Arrays in structs (continued)

  • 7/28/2019 Record - Struct

    17/29

  • 7/28/2019 Record - Struct

    18/29

    18

    structs in Arrays

  • 7/28/2019 Record - Struct

    19/29

  • 7/28/2019 Record - Struct

    20/29

  • 7/28/2019 Record - Struct

    21/29

    Structure: Initialization

  • 7/28/2019 Record - Struct

    22/29

    Structure: Accessing the element

    Structure member operator access via . andbehave like a normal variable.

    typedef struct {

    int id

    char name[30];int semester;float cgpa;

    }STUDENT;

    STUDENT stud1;//declaration

    stud1.id = 1234;

    stud1.cgpa = 3.45;

    strcpy( stud1.name, "Ahmad Al-Bab");

    printf (%s CGPA is %f,stud.name ,stud.cgpa);

  • 7/28/2019 Record - Struct

    23/29

    Structure and Array

    STUDENT stuAry[50];

  • 7/28/2019 Record - Struct

    24/29

    Structure and Array: Example#include

    #include

    #define size 3typedef struct {

    int ID;

    float Test1;

    } STUDENT;

    int main()

    {

    STUDENT student[3];

    int i;

    for (i = 0; i < size; i++)

    {

    scanf ("%d", &student[i].ID);

    scanf ("%f", &student[i].Test1);

    }

    for (i = 0; i < size; i++)

    {

    printf ("The student ID %d", student[i].ID);

    printf ("got test 1 %f \n", student[i].Test1);

    }

    getch();

    return 0;

    Global declaration

    Local Declaration

    Store records

    Display records

  • 7/28/2019 Record - Struct

    25/29

    Structure and Function

    Structure can be the parameter of function

    Same concept with passing an array

    Can either element of the structure or the whole

    structure

  • 7/28/2019 Record - Struct

    26/29

    Structure and Function: Example#include

    #include

    typedef struct {int ID;

    float Test1;

    } STUDENT;

    void readSturcture (STUDENT student []);

    int main()

    {

    STUDENT student[3]; //variable declaration

    int i;readSturcture(student);

    for (i = 0; i < 3; i++)

    {

    printf ("ID\tTest1\n");

    printf ("%d\t",student[i].ID);

    printf ("%6.2f\t",student[i].Test1); }

    getch();

    return 0;

    }

    void readSturcture (STUDENT student [])

    {

    int i;

    for (i = 0; i < 3; i++)

    {

    printf ("enter student ID, Test1 with index %d : ",i);

    scanf ("%d",&student[i].ID); }

    }

    Pass the array structure ofstudent;

  • 7/28/2019 Record - Struct

    27/29

    Exercise

    Define a data structure STUDENT that can

    accommodate the table below.

    Initialize three variables std1, std2 and std3

    to the values shown in the table below.

    ID Name Year CGPA

    E2345 Ahmad Al-Bab 6 3.89C1234 James Bomb 5 3.78

    M3211 Jay Loh 8 3.67

  • 7/28/2019 Record - Struct

    28/29

    28

    Summary

    struct: collection of a fixed number of

    components

    Components can be of different types

    Called members

    Accessed by name

    struct is a reserved word

    No memory is allocated for a struct

    Memory when variables are declared

  • 7/28/2019 Record - Struct

    29/29

    29

    Summary (continued)

    Dot (.) operator: member access operator

    Used to access members of a struct

    The only built-in operations on a struct arethe assignment and member access

    struct can be passed by value or reference

    A function can return a value of type struct