Computer Programming C: UNIT IV: Structures and Unions

Simple Example C Programs using Structures and Unions

Computer Programming C: Structures and Unions: Programs using Structures & Unions

CASE STUDIES

(Programs using Structures & Unions)

 

1. Write a program to transfer a structure to a function by passing the structure address (a pointer) to the function.

In the structure variable std is a static structure of type record. Whose members are assigned an initial set of values.

The initial values are displayed when the program begins to execute. Then the structure address is passed to the function fun(), where different values are assigned to the member of the structure. With in fun() note that the formal argument declaration that defines 'pt' as a pointer to a structure of type record.

/* Program to transfer a structure to a function by passing the structure address (a pointer) to the function */

#include <stdio.h>

/* Global declarations */

typedef struct

{

        char* name;

        int no;

        int age;

} record;

main()

{

        * Local definitions */

        void fun(record *pt);

        static record std = {"LAK", 15, 25};

        /* Statements */

        clrscr();

        printf("%s\t%d\t%d", std. name, std.no, std. age);

        fun(&std);

        printf("\n %s\t%\d\t%d", std. name, std. no, std. age);

        getch();

} /* main */

void fun(record *pt)

{

        pt‒>name = "MUNI";

        pt‒>no = 16;

        pt‒> age = 26;

} /* fun() */

OUTPUT

LAK  15  25

MUNI 16  26

EXPLANATION: In the above program, the structure record is defined with integer and character type variable. The main function gives initialised values {"LAK",15, 25} as output. Then the structure address is passed to the function fun(), where values are assigned MUNI,16,26 respectively as name, no and age. These values are returned to the main function and output has been printed.

 

2. Write a program to pass an array of structure to a function, and return a pointer to a structure.

Eg: Locate and display a student record, each record will be maintained in a structure.

The basic strategy will be to enter a student number, and then transform array of records to the function called search(). Within the search() function the specified student number will be compared with the student number that is stored within each student record until a match is found, or until the entire list of records has been searched.

If a match is found, a pointer to that array element is returned to main() and the contents of their records are displayed.

 /* Program to pass an array of structure to a function and return a pointer to a structure */

#include <stdio.h>

#define N 3

#define NULL 0

/* Global declarations */

typedef struct

{

      int sno;

      char* name;

      float age;

} record;

void main()

{

/* Local definitions */

static record std[N] ={{15, "MUNI", 28},{16, "LAK",27}, {17, "RAJA",31}

};

int stdno;

record *pt;

record search(record table[], int stdno);

/*Statements */

printf("Student Number Locater");

printf("\nTo exit the program enter 0 for student number");

printf("\nEnter Student Number: ");

scanf("%d", &stdno);

while(stdno!=0)

{

pt = search(std, stdno);

if (pt!= NULL)

{

printf("\nNumber.....%d",pt‒>sno);

printf("\nName.......%d",pt‒>name);

printf("\nAge........%d", pt‒>age);

} /* if */

else

printf("\nNot found...try again");

printf("\nEnter Student Number : ");

scanf("%d", &stdno);

} /* while */

} /* main */

Record *search(record table[], int stdno)

{

int count;

for (count=0; count < N; ++count)

if(table[count].sno==stdno)

return(&table[count]);

return(NULL);

} /* record *search() */

OUTPUT

Student Number Locater

To exit the program enter 0 for student number

Enter Student Number: 15

Number.....15

Name.......MUNI

Age........27

Enter Student Number: 0

EXPLANATION: This program refers to pass an array of structure to a function, and return a pointer to a structure. In the program structure record and static record std[N] is initialised. The program reads the stdno from keyboard, and checks the condition using while loop (stdno!=0). If condition is true then if statement checks the condition pt!=NULL, while the condition is true then statement is printed otherwise else block is executed and prints the block of statement.

 

3. Write a program to pass union to a function.

Eg: Raise a number to a power.

Evaluate y=xn, where 'x' and 'y' are floating point values and 'n' can be either float or int. If 'n' is an integer, then 'y' can be evaluated by multiplying 'x' by itself an appropriate number of times.

/* Program to pass union to a function.*/

#include <stdio.h>

#define N 3

#define NULL 0

/* Global declarations */

typedef union

{

    float aa;

    int bb;

}nvals;

typedef struct

{

    float x;

    char flag;

    nyals exp;

} values;

void main()

{

/* Local definitions */

values a;

float power(values a);

int i;

float n,y;

/* Statements */

printf("Enter a value for X (ie., x power n)...........");

scanf("%f",&a.x);

printf("\nEnter a value for N(ie., x power n).............");

scanf("%f", &n);

i= (int)n;

a.flag=(i==n)?'i': 'f';

if(a.flag== 'i')

        a.exp.bb=i;

else

        a.exp.aa=n;

if(a.flag=='f'&&a,x<=0.0)

{

          printf("can't raise a nonpositive number to a");

          printf("\nfloating point power");

}

else

{

          y=power(a);

          printf("\ny = %.4f",y);

} /* if..else */

} /* main */

float power (values a)

{

          int i;

          float y = a.x;

          if(a.flag== 'i')

          {

                    if(a.exp.bb==0)

                    y=1.0;

                 else

                 {

                    for (i=1;i<abs(a.exp.bb); ++i)

                              y=a.x;

                    if (a.exp.bb<0)

                              y=1./y;

                 } /* if..else */

} /* if */

else

y=exp(a.exp.aa*log(a.x));

return(y);

} /* power() */

OUTPUT

Enter a value for X (ie., x power n).....5

Enter a value for N(ie., x power n).....2

y  = 25.0000

EXPLANATION: This program refers to passing union to a function. The union nvals and structure values are declared initially. The main function reads the variable n from the terminal and check the condition a. flag='i' if yes then initialise a.exp.bb=i else a.exp.aa=n. if statement is executed and prints the true block otherwise prints the else block of statement. In function power() if statement checks the conditions a.flag==i. If condition is true then true block is executed or else block is executed and returned a value to the main program.

 

4. Write a program to print name, and address using structure.

/* Program to print name, and address */

#include <stdio.h>

#include<conio.h>

struct address /* structure declaration */

{

       char plot[30]; /* members */

       char street[30];

       char city[30];

};

struct student /* structure declaration */

{

       char name[30]; /* members */

       int marks;

       struct address adr;

};

void main()

{

struct student class[5]; /* Local definitions*/

/* statements "/

clrscr();

printf("Enter Name: ");

scanf("%s", class[1].name);

printf("Enter Marks: ");

scanf("%d", &class[1]. marks);

printf("Enter Plot Number: ");

scanf("%s", class[1].adr.plot);

printf("Enter Street name: ");

scanf("%s", class[1].adr. street);

printf("Enter City Name : ");

scanf("%s", class[1].adr.city);

printf("\n");

printf("Name is : %s\n", class[1].name);

printf("Marks are%d\n", class[1].marks);

printf("Plot Number is: %s\n", class[1].adr.plot);

printf("Street: : %s\n", class[1].adr.street);

printf("City is %s\n", class[1].adr.city);

getch();

} /* main */.

OUTPUT

Enter Name : Mahi

Enter Marks : 89

Enter Plot Number : 68

Enter Street name : 24

Enter City Name : Chennai

Name is : Mahi

Marks are : 89

Plot Number is : 68

Street : 24

City is : Chennai

EXPLANATION: This program illustrates the concept of nested structure and inputting values for two different structure member and printing their contents.

 

Computer Programming C: UNIT IV: Structures and Unions : Tag: Computer Science : - Simple Example C Programs using Structures and Unions


Computer Programming C: UNIT IV: Structures and Unions



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation