C - Structure

 

C Language

Structure



Fill in the blanks

1.Write a keyword for Structure   struct

2.Write a keyword for Union   union

3.Structure is collection of different data type

4.Structure is collection of similar data type .[T/F]  False

5.  dot( . ) operator used to access a structure Member

6.Write a Symbol for referencing operator  ->

7.Write a Syntax for Object struct struct_type  object;

8.Union will occupy memory for all variable .[T/F] . False

9. Structure will store in Continuous Memory Location .[T/F] . False

10.The size of a structure can be determined by sizeof  operator

 

Answer the following Question

                                                                    (4 marks)

1.Write a program to print today date using Structure

#include<stdio.h>

#include<conio.h>

struct date

{

int month;

int day;

int year;

};

main()

{

struct date today;  //creating on object to accessing structure element

today.month=10;

today.day=14;

today.year=1995;

clrscr();

printf("Today date is %d / %d /  %d",today.month,today.day,today.year);

getch();

}

2.Write a program for Array of Structure.

#include<stdio.h>

#include<conio.h>

main()

{

int i;

clrscr();

struct emp

{

int eno;

char ename[20];

int sal;

};

struct emp e[5];

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

{

printf("enter the %d  eno,ename,sal",i);

scanf("%d%s%d",&e[i].eno,&e[i].ename,&e[i].sal);

}

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

{

printf(" %d  %s  %d\n",e[i].eno,e[i].ename,e[i].sal");

}

getch();

}

3.Write a difference between Structure and Array.

ARRAY

STRUCTURE

Collections of similar data types stores in continuous memory location

Collections of different data types

Syntex

Datatype variablename[size]={value};

Syntex

struct struct_type

{

Member_type1 member_name1;

Member_type2 member_name2;

...............

............

};

Example

int a[5]={10,20,30,40,50};

Example

struct account

{

int acct_no;

short acct_type;

char name[30];

char street[30];

char city_state[30];

long balance;

long last_payment;

};

4.Write a Program for Referencing member using Pointer


 #include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

    int n=3333;

    char t='C';

    int b=99;

    struct account

    {

    int acct_no;

    short acct_type;

    char name[30];

    char street[30];

    char city_state[30];

    int balance;

    int last_payment;

    }customer,*pc=&customer;

    customer.acct_no=n;

    customer.acct_type=t;

    strcpy(customer.name,"Sanjay");

    customer.balance=b;

    clrscr();

printf("%d  %c  %s  %d\n",pc->acct_no,pc->acct_type,pc->name,pc->balance);

    getch();

}

Answer the following Question

                                                                    (10 marks)

          1.Write a Program for Nested Structure

#include<stdio.h>

#include<conio.h>

struct date

{

    int month,day,year;

};

struct time

{

    int hours,mins,secs;

};

struct date_time

{

    struct date sdate;

    struct time stime;

};

static struct date_time today={{2,11,1985},{3,3,33}};

void main()

{

    clrscr();

    printf("Month=%d\n",today.sdate.month);

    printf("Day=%d\n",today.sdate.day);

    printf("Year=%d\n",today.sdate.year);

    printf("Hours=%d\n",today.stime.hours);

    printf("Minutes=%d\n",today.stime.mins);

    printf("Seconds=%d",today.stime.secs);

    getch();

}

          2.Write a Program for Pointer to Structure

#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

    int n=3333;

    char t='C';

    int b=99;

    struct account

    {

    int acct_no;

    short acct_type;

    char name[30];

    char street[30];

    char city_state[30];

    int balance;

    int last_payment;

    }customer,*pc=&customer;

    customer.acct_no=n;

    customer.acct_type=t;

    strcpy(customer.name,"Sanjay");

    customer.balance=b;

    clrscr();

printf("%d  %c  %s  %d\n",pc->acct_no,pc->acct_type,pc->name,pc->balance);

    getch();

}

 

 

No comments:

Post a Comment