← Back to C++ Course | Chapter 5: Functions | Lesson 5 of 13

C++ Passing Structures to Functions

A struct can be passed to a function by value (as a copy), by reference (to modify the original), or by const reference (for efficient read-only access), each suited to a different use case.
Syntax
cpp
struct StructName {
  type member;
};

void function_name(StructName s);   // by value (copy)
void function_name(StructName &s);  // by reference

Passing a Struct by Value

Passing a struct by value gives the function its own complete copy of the struct, so any changes made inside the function have no effect on the caller's original struct.

Example: Passing a Struct by Value

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

void tryModify(Point p) {
	p.x = 100;
}

// Program execution starts in main()
int main() {
	Point pt = {1, 2};
	tryModify(pt);
	// Print to the console with cout
	std::cout << pt.x << "," << pt.y << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Passing a Struct by Reference

Passing a struct by reference, using &, lets the function modify the caller's original struct directly, since the parameter refers to the same memory rather than a copy.

Example: Passing a Struct by Reference

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

void modify(Point &p) {
	p.x = 100;
}

// Program execution starts in main()
int main() {
	Point pt = {1, 2};
	modify(pt);
	// Print to the console with cout
	std::cout << pt.x << "," << pt.y << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Passing a Struct by const Reference

Passing a struct by const reference avoids the cost of copying a potentially large struct while also preventing the function from modifying the caller's original data, combining efficiency with safety.

Example: Passing a Struct by const Reference

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

void printPoint(const Point &p) {
	// Print to the console with cout
	std::cout << p.x << "," << p.y << std::endl;
}

// Program execution starts in main()
int main() {
	Point pt = {5, 7};
	printPoint(pt);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Returning a Struct from a Function

A function can also return a struct by value, constructing and handing back a complete struct to the caller, which is a common way to bundle several related results together.

Example: Returning a Struct from a Function

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

Point makeOrigin() {
	Point p;
	p.x = 0;
	p.y = 0;
	// Send p back to the caller
	return p;
}

// Program execution starts in main()
int main() {
	Point origin = makeOrigin();
	// Print to the console with cout
	std::cout << origin.x << "," << origin.y << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Passing an Array of Structs

An array of structs decays to a pointer when passed to a function, just like an array of any other type, so the function typically also receives the array's length as a separate parameter.

Example: Passing an Array of Structs

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

void printAll(Point points[], int size) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < size; i++) {
		// Print to the console with cout
		std::cout << points[i].x << "," << points[i].y << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
}

// Program execution starts in main()
int main() {
	Point pts[] = {{1, 2}, {3, 4}};
	printAll(pts, 2);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing a big struct by value each time, which copies everything and slows the program.
  2. Modifying a struct passed by value and expecting the original to change.
  3. Forgetting const when the function should only read the struct.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.