Code Snippets C/C++ Code Snippets

C++ - Program to demonstrate example of Passing Structure within a function.

By: IncludeHelp, On 30 SEP 2016

In this example we will learn how we can pass a structure within a function?

Here we will pass a structure into the function and access function (function calling) as call by reference, using call by reference we can get changes made inside the function with structure.

In this example there is a structure named student with two variables name and rollNumber, we will read number of students to read.

We will read N student’s record through read() function and print entered value trough print() function; in both functions structure variables will be passed as call by reference.

C++ program - Demonstrate Example of Passing Structure within a Function

/*C++ - Program to demonstrate example of Passing 
Structure within a function.*/

#include <iostream>
#include <iomanip>

using namespace std;

#define MAX 100
struct student{
	char name[30];
	int rollNumber;

void read(struct student &s)
	     cout<<"Enter name:";
		cout<<"Enter roll number:";

void display (struct student s)

int main(){
	struct student std[MAX];
	int n,loop;

	cout<<"Enter total number of students: ";

	//read n records
	for(loop=0; loop<n; loop++){
	//print all records
	cout<<"Entered records are:"<<endl;
	cout<<setw(30)<<"Name"<<setw(20)<<"Roll Number"<<endl;	
	return 0;
Enter total number of students: 2
Enter name:Mike
Enter roll number:101
Enter name:Monty            
Enter roll number:102
Entered records are:
                     Name         Roll Number
                          Mike       101
                         Monty       102   

Copyright © 2024 All rights reserved.