C++ Passing Structures to Functions
In this page:
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
#include <iostream>
struct Point { int x; int y; };
void tryModify(Point p) {
p.x = 100;
}
int main() {
Point pt = {1, 2};
tryModify(pt);
std::cout << pt.x << "," << pt.y << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
struct Point { int x; int y; };
void modify(Point &p) {
p.x = 100;
}
int main() {
Point pt = {1, 2};
modify(pt);
std::cout << pt.x << "," << pt.y << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
struct Point { int x; int y; };
void printPoint(const Point &p) {
std::cout << p.x << "," << p.y << std::endl;
}
int main() {
Point pt = {5, 7};
printPoint(pt);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
struct Point { int x; int y; };
Point makeOrigin() {
Point p;
p.x = 0;
p.y = 0;
return p;
}
int main() {
Point origin = makeOrigin();
std::cout << origin.x << "," << origin.y << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
struct Point { int x; int y; };
void printAll(Point points[], int size) {
for (int i = 0; i < size; i++) {
std::cout << points[i].x << "," << points[i].y << " ";
}
std::cout << std::endl;
}
int main() {
Point pts[] = {{1, 2}, {3, 4}};
printAll(pts, 2);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions