Computer Programming C Laboratory: Unions C Programs

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

C Programs using Structures & Unions

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.

Computer Programming C Laboratory: Unions C Programs : Tag: C Programming : - Write a program to transfer a structure to a function by passing the structure address (a pointer) to the function


Computer Programming C Laboratory: Unions C Programs



Under Subject


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 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