Saturday, June 17, 2023

C language - Structure

 

STRUCTURES

COLLECTIONS OF DIFFERENT DATA TYPE

 

DIFFERENCE BETWEEN ARRAY & STRUCTURE

ARRAY

STRUCTURE

Collections of similar data types stores in continuous memory location

Collections of different data types

Syntax

Datatype variablename[size]={value};

Syntax

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;

};

 

 

object

object- memory

object syntex

struct struct_type  object;

 

PROGRAM 1

#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();

}

 

output

Todays date  is  10/14/1995

 

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();

}

output

Enter  the  0  eno,ename,sal

1

raja

3000

Enter  the  1  eno,ename,sal

2

kumar

8000

Enter  the  2  eno,ename,sal

3

babu

9000

Enter  the  3  eno,ename,sal

4

suresh

5000

Enter  the  4  eno,ename,sal

5

ganesh

6000

 

1  raja         3000

2  kumar    8000

3  babu       9000

4  suresh     5000

5  ganesh    6000

String  Function using  Structure

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct month

{

int  d;

char  name[4];

};

 

main()

{

struct month a;

clrscr();

a.d=15;

strcpy(a.name,"Jan");

printf("The month %s has %d  days",a.name,a.d);

getch();

}

Output

The month Jan has  15 days

 

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();

}

Output

Month=2

Day=11

Year=1985

hours=3

mins=3

sec=33

Size of the  Structure

#include<stdio.h>

#include<conio.h>

main()

{

    struct sample

    {                  // byte value

        char a;        //  char -1

        int b;         //  int  -2

        char c;        //  char -1

        float d;       //  float-4

    };

struct sample variable;

printf("sizeof char=%d,int=%d, float=%d\n ",sizeof(char),sizeof(int),sizeof(float));

printf("size of struct sample=%d\n",sizeof(struct sample));

getch();

}

 

output

size of char=1 , int=2  , float=4

size of struct sample= 8

Pointers  To Structures

#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();

}

 

Output

3333 C  Sanjay  99

 

Passing  Structure To  Functions

#include<stdio.h>

#include<conio.h>

struct person

{

    char *name;

    int age;

};

struct person fun(struct person);

main()

{

    struct person exam={"cpp",15};

    clrscr();

    printf("before calling function\n");

    printf("%s\n%d\n",exam.name,exam.age);

    exam=fun(exam);

    printf("after calling function\n");

    printf("%s\n %d\n",exam.name,exam.age);

    getch();

}

struct person fun(struct person exam1)

{

    exam1.name="c language";

    exam1.age=17;

    return(exam1);

}

 

 

Output

before calling function

cpp

15

after calling function

c language

17

Unions

keyword: union

Disadvantage:

Union only occupies memory for highest data variable.but  Structure occupies memory for all data variable.

syntex

union  union type

{

member_type1  member_name1;

member_type2  member_name2;

member_type3  member_name3;

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

};

 

PROGRAM

#include<stdio.h>

#include<conio.h>

union  num

{

double  d;

int  i;

}NUM;

main()

{

NUM.d=3.14;

NUM.i=5;

clrscr();

printf("%u",sizeof(NUM));

printf("\n  %lf\n %d",NUM.d,NUM.i);

getch();

}

output

8

3.14

5

0 comments:

Post a Comment