Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Passing Structs to Functions | Struct Fundamentals
Quizzes & Challenges
Quizzes
Challenges
/
C++ Structures and Enumerations

bookPassing Structs to Functions

When you work with structs you often need to pass them to functions. There are three primary ways to do this: by value, by pointer, and by reference. Each method determines whether changes made inside the function affect the original struct or just a copy:

Passing by value
expand arrow

When you pass a struct by value, the function receives a copy of the struct. Any changes made to the struct inside the function only affect the copy, not the original struct. This means the original data stays unchanged outside the function.

Passing by pointer
expand arrow

Passing a struct by pointer gives the function the memory address of the struct. The function can then modify the original struct by accessing it through the pointer. Any changes made inside the function will affect the actual struct outside the function.

Passing by reference
expand arrow

Passing by reference allows the function to work with the actual struct directly, without making a copy. The function can access and modify the original struct, and all changes will be reflected outside the function. This method is efficient and avoids unnecessary copying.

You should choose how to pass a struct to a function based on what you want your code to do. If you want to protect the original data and only work with a copy, pass the struct by value.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> #include <string> struct Person { std::string name; int age; }; void updateByValue(Person p) { p.age += 1; std::cout << "Inside updateByValue: " << p.name << " is now " << p.age << "\n"; } int main() { Person person{"Alice", 30}; std::cout << "Original: " << person.name << " is " << person.age << "\n"; updateByValue(person); std::cout << "After updateByValue: " << person.name << " is " << person.age << "\n"; }

This is useful for small structs or when you do not want the function to change the original. If you want the function to modify the original struct, or if the struct is large and copying it would be inefficient, pass by pointer.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> #include <string> struct Person { std::string name; int age; }; void updateByPointer(Person* p) { p->age += 1; std::cout << "Inside updateByPointer: " << p->name << " is now " << p->age << "\n"; } int main() { Person person{"Alice", 30}; std::cout << "Original: " << person.name << " is " << person.age << "\n"; updateByPointer(&person); std::cout << "After updateByPointer: " << person.name << " is " << person.age << "\n"; }

Passing by reference is often preferred for clarity and safety, unless you specifically need to use pointers (for example, when working with dynamic memory or nullable arguments). In the Person example above, passing by value leaves the original person unchanged, while passing by pointer or by reference allows the function to update the original struct's age.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> #include <string> struct Person { std::string name; int age; }; void updateByReference(Person& p) { p.age += 1; std::cout << "Inside updateByReference: " << p.name << " is now " << p.age << "\n"; } int main() { Person person{"Alice", 30}; std::cout << "Original: " << person.name << " is " << person.age << "\n"; updateByReference(person); std::cout << "After updateByReference: " << person.name << " is " << person.age << "\n"; }
question mark

Which statement correctly describes the effect of passing a struct to a function by value, pointer, and reference?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain the differences between passing by value, pointer, and reference with code examples?

When should I use each method in practice?

What are the pros and cons of each approach?

Awesome!

Completion rate improved to 8.33

bookPassing Structs to Functions

Pyyhkäise näyttääksesi valikon

When you work with structs you often need to pass them to functions. There are three primary ways to do this: by value, by pointer, and by reference. Each method determines whether changes made inside the function affect the original struct or just a copy:

Passing by value
expand arrow

When you pass a struct by value, the function receives a copy of the struct. Any changes made to the struct inside the function only affect the copy, not the original struct. This means the original data stays unchanged outside the function.

Passing by pointer
expand arrow

Passing a struct by pointer gives the function the memory address of the struct. The function can then modify the original struct by accessing it through the pointer. Any changes made inside the function will affect the actual struct outside the function.

Passing by reference
expand arrow

Passing by reference allows the function to work with the actual struct directly, without making a copy. The function can access and modify the original struct, and all changes will be reflected outside the function. This method is efficient and avoids unnecessary copying.

You should choose how to pass a struct to a function based on what you want your code to do. If you want to protect the original data and only work with a copy, pass the struct by value.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> #include <string> struct Person { std::string name; int age; }; void updateByValue(Person p) { p.age += 1; std::cout << "Inside updateByValue: " << p.name << " is now " << p.age << "\n"; } int main() { Person person{"Alice", 30}; std::cout << "Original: " << person.name << " is " << person.age << "\n"; updateByValue(person); std::cout << "After updateByValue: " << person.name << " is " << person.age << "\n"; }

This is useful for small structs or when you do not want the function to change the original. If you want the function to modify the original struct, or if the struct is large and copying it would be inefficient, pass by pointer.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> #include <string> struct Person { std::string name; int age; }; void updateByPointer(Person* p) { p->age += 1; std::cout << "Inside updateByPointer: " << p->name << " is now " << p->age << "\n"; } int main() { Person person{"Alice", 30}; std::cout << "Original: " << person.name << " is " << person.age << "\n"; updateByPointer(&person); std::cout << "After updateByPointer: " << person.name << " is " << person.age << "\n"; }

Passing by reference is often preferred for clarity and safety, unless you specifically need to use pointers (for example, when working with dynamic memory or nullable arguments). In the Person example above, passing by value leaves the original person unchanged, while passing by pointer or by reference allows the function to update the original struct's age.

main.cpp

main.cpp

copy
123456789101112131415161718192021
#include <iostream> #include <string> struct Person { std::string name; int age; }; void updateByReference(Person& p) { p.age += 1; std::cout << "Inside updateByReference: " << p.name << " is now " << p.age << "\n"; } int main() { Person person{"Alice", 30}; std::cout << "Original: " << person.name << " is " << person.age << "\n"; updateByReference(person); std::cout << "After updateByReference: " << person.name << " is " << person.age << "\n"; }
question mark

Which statement correctly describes the effect of passing a struct to a function by value, pointer, and reference?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3
some-alt