C Programs using Structures & Unions
Write a C 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.
/* C 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.
Computer Programming C Laboratory: Unions C Programs : Tag: C Programming : - Write a C program to pass an array of structure to a function, and return a pointer to a structure
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
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