c program day-8

Upload: eshamu

Post on 04-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 C Program Day-8

    1/17

  • 7/31/2019 C Program Day-8

    2/17

    Define Structure

    Create Structure variable

    Access structure member using . operator

    Array of Structures, passing structure to function

    Nested Structures

  • 7/31/2019 C Program Day-8

    3/17

    Structure are collection of different datatype

    grouped together under a single variable name for

    convienient handling. 'struct' is a keyword, struct type

    is a name which identifies the structure.syntax : struct

    {

    datatype member1;

    datatype member2;};

    struct book{

    int pages;char bookname[10];

    char author[20];

    float price;

    };

  • 7/31/2019 C Program Day-8

    4/17

    J A V A H A U N T E R 467

    Book Name Author Name Pages

  • 7/31/2019 C Program Day-8

    5/17

    Individual structure type variables can be declared asfollows:

    Syntax :struct structurename variable1, variable2, ..... variable n;

    ExampleStruct book b1; /* b1 is a structure variable name */

    Member variables can be accessed using the dot or periodoperator as follows:

    Syntax :

    Stru_variablename.membername;

    ExampleB1.pages=400;B1.price=450.00;

  • 7/31/2019 C Program Day-8

    6/17

    Write a Program the create the Account Details using Structure?

    #include

    struct amount

    {

    int ac_no;

    char name[10];

    int balance;

    };

    void main(){

    struct amount v;

    printf("\nEnter the account Details");

    scanf("%d%s%d",&v.ac_no,&v.name, &v.balance);printf("The values are %d%s%d",v.ac_no,

    v.name, v.balance);

    printf("%d",sizeof(struct amount));

    }

  • 7/31/2019 C Program Day-8

    7/17

    Write a program to create student structure#include

    struct stud

    {

    int id;char name[20];

    int mark1,mark2,mark3;

    int total;

    int avg;

    }b;void main()

    {

    printf("\nEnter the student details");

    scanf("%d %s %d %d %d",&b.id,&b.name,

    &b.mark1,&b.mark2,&b.mark3);

    b.total=b.mark1+b.mark2+b.mark3;

    b.avg=b.total/3;

    printf("%d %s %d %d %d",b.id,b.name, b.mark1, b.mark2,

    b.mark3);}

  • 7/31/2019 C Program Day-8

    8/17

    Initializing StructuresLike variables and arrays, structure variables can be initialized at the

    beginning of the program.

    Consider the following program :The variable emp1 and emp2 of the type employee can be initialized

    when it is declared as

  • 7/31/2019 C Program Day-8

    9/17

    Array of structures are defined as a group of data ofdifferent data types stored in a consecutive memory location with a

    common variable name.

    For Example,

    struct employee

    {

    int empno;

    char empname[20];

    char deptname[10];

    float salary;

    }emp[5];

  • 7/31/2019 C Program Day-8

    10/17

    Passing Structures as Arguments

    A structure variable can be passed as an argument toanother function

    This is a useful facility and it is used to pass groups of

    logically related data items together instead of passing

    them one by one.

  • 7/31/2019 C Program Day-8

    11/17

    /*PASSING STRUCTURES TO FUNCTION ARGUMENTS*/

    #include

    struct record

    {

    char *name;

    int acct_no;

    char acct_type;

    float bal;};

    struct record fun(struct record);

    void main(){

    struct record c;

    printf("\n Enter Details");

    scanf("%s%d%c%f",c.name,&c.acct_no, &c.acct_type, &c.bal);

    c=fun(c); //calling

    printf("\n%s %d %c %f",c.name,c.acct_no,c.acct_type,c.bal);

    }

  • 7/31/2019 C Program Day-8

    12/17

    struct record fun(struct record r)

    {r.name="Raja";

    r.acct_no=1567;

    r.acct_type='R';

    r.balance=900.00;return(r);

    }

  • 7/31/2019 C Program Day-8

    13/17

    Passing the structure member as aparameter to the function#include#includetypedef struct stud

    {int roll_no;char name[20];float marks;}student;student st;

    void print_rec(int,char [],float);void main(){char ans;clrscr();do

    {printf("\n \t Enter the record");printf("\n Enter the Roll Number");scanf("%d",&st.roll_no);

    printf("\n Enter the Name");scanf("%s",st.name);

    printf("\n Enter the Marks");scanf("%f",&st.marks);print_rec(st.roll_no,st.name,st.marks);printf("\n Do U Want to store the More

    record ");ans=getche();}while(ans=='y');}

    void print_rec(int r,char n[],float m)

    {printf("\n You have Entered Followingrecord");printf("\n %d %s %f",r,n,m);}

  • 7/31/2019 C Program Day-8

    14/17

    Nested Structure

    A structure within another structure is called Nesting of

    structures.The declaration of the embedded structure must appear before

    the declaration of the outer structure.

    Example

    Struct date

    {

    int month;

    int day;int year;

    };

    struct account

    {

    int acc_no;char name[40];

    char acc_type;

    float balance;

    struct date dob;

    } customer;

  • 7/31/2019 C Program Day-8

    15/17

    /* NESTED STRUCTURES

    A structure within another

    structure */

    #includestruct dob {

    int date;

    int month;

    int year; };

    struct stud{

    int sno;

    char sname[10];

    struct dob sdob;

    char sex; };void main()

    {

    struct stud s;

    clrscr();

    printf("enter the sno\n");

    scanf("%d",&s.sno);

    printf("enter the sname\n");

    scanf("%s",s.sname);printf("enter the dob\n");

    scanf("%d%d%d %c",&s.sdob.date,

    &s.sdob.month,&s.sdob.year,&s.sex);

    printf("%d\t %s\t %d %d %d \t

    %c", s.sno, s.sname, s.sdob.date,

    s.sdob.month, s.sdob.year, s.sex);

    getch();

    }

  • 7/31/2019 C Program Day-8

    16/17

    Session Summary

    The keyword struct begins the structure definition followed by the tag(i.e strcture

    name) within the braces the structure members are declared

    The structure definition create a new data type that can be used to declare variables.

    A member of structure is always referred and accessed using the member accessoperator(.) via the structure variable name

    A structure containing a member that is a pointer to the same structure type is called

    as a self referential structure.

    The structure pointer operator ( ) is used when accessing a member of a structure via

    a pointer to the structure

  • 7/31/2019 C Program Day-8

    17/17

    EXERCISES

    1. Define a structure in C that contains the following:

    a. An integer quantity called Won

    b. An integer quantity called Lost

    c. A floating quantity called Lost

    d. An array of characters to hold the name

    Include the user defined data type record within the definition?

    2. Define a structure called student that will describe the following information student

    name,class,rollno, subject marks & total. Using student declare an array stu_list

    with 30 elements. Write program in C to read the information about all the 30

    students and to display the information?