Another clear explanation; it makes so much more sense to call it pass-by-copy rather than pass-by-value. It makes the difference between this and pass-by-reference explicit. And, throwing in the bonus points about costs of one vs the other and the benefit of const reference is truly helpful!
Makes sense. It's why if you use constant references, you can have the best of both worlds - performance increase of using reference variables and the safety of pass-by-value. 👍 Thanks for the comment, I really appreciate it. Happy holidays!
Damn this example is straight from the snippet of the course book my teacher used as material for the structs. The only thing that's missing from this is when you make the struct an array and pass it as argument to a function and print the information of multiple structs. For example the classic student struct but what good is having a student struct? You want a big database of student information so obviously it's gonna have to be a struct. However for some reason it seems like it's almost impossible to find a tutorial video that goes through such a fundamental skill. Or even to find an example from google that uses them in that way (that seems very natural for solving real problems). Like sure it plays by the rules of passing struct and array as arguments, but it's really difficult to wrap your head first around structs and then trying to combine it to an array in syntax when you also pass it as an argument. Because it isn't as complicated as you'd assume, but you also would have to really understand what each part of the functions/methods/whatever mean. Like when do you have to call the general struct by name, when do you need to call the unique struct you created by name, when do you have to put a number to define or point to the array and when do you leave the brackets completely out. It's very simple ONCE you see how it works, but it's almost impossible to find an example or figure it out by yourself. I'll give an example: struct Student{ Let's not care about what's here}; Student createStudent(){just a function}; void printStudent(Student student[size], int count); // this one is a void function that takes the created student struct of Student that has the number of "size" members (of course it has to be static because it's an array in c++) in main you would do for (int i = 0; i < size; ++i) { student1[i] = createStudent(); } printStudent(student1, size); Like this really messed me up with the struct "Student", the struct "Student" as the value of the "createStudent()" function, argument "Student student[size]", creating and defining "student1[i] = createStudent()" and then the argument "student1". If you really understand what each of those are from variables to structs to return values of function being just the Struct type to arguments having this or that information in definition but passed with no syntax, but creating it with syntax and having yet another name. The examples don't usually make it much easier when the repetitive naming is so common (like Student student and student1[i]) without explaining what each statement means and what is a type that you can use as a value and what is just a variable etc etc.
Hi, thxs foe your valuable content. I cant do a struct with func which is struct define with an array [] #include #include using namespace std; //declaration of struct struct PLAYER { int id; string name; }; //func to enter the infos void infos(PLAYER& x) { for (auto c = 0; c < 3; ++c) { cout > x.name; x.id = c + 1; //enter the ID standart with auto increment } } //search the name with an ID int SELECTnameWHEREid(int index, PLAYER& x) { for (auto& s : x) { if (x.id == index) cout
That's because your parameters for your functions are for structs, and not for arrays. You need an array parameter. PLAYER x[] Would do it. Also, quick tip: all uppercase characters are usually reserved for named constants, as in your first line of main. Usually, a struct will be named like this: Player
Another clear explanation; it makes so much more sense to call it pass-by-copy rather than pass-by-value. It makes the difference between this and pass-by-reference explicit.
And, throwing in the bonus points about costs of one vs the other and the benefit of const reference is truly helpful!
Glad you liked it!
You are legitimately a GODSEND. This explanation cleared up all my confusion about passing structs by reference. Keep up the phenomenal work!! :)
Glad it helped!
Excellent work. I lije how you get to the point and not spend time on superfluous stuff.
I appreciate that!
Спасибо
Super explanation! Thank you.
You are welcome!
Good tutorial. It is good to know you can pass by reference but I would rarely do that as it makes debugging a nightmare on a large project.
Makes sense. It's why if you use constant references, you can have the best of both worlds - performance increase of using reference variables and the safety of pass-by-value. 👍
Thanks for the comment, I really appreciate it. Happy holidays!
Thank you so much !
I found out that even with Struct& reference the original struct defined in main() didn't change for some reason.
Damn this example is straight from the snippet of the course book my teacher used as material for the structs. The only thing that's missing from this is when you make the struct an array and pass it as argument to a function and print the information of multiple structs. For example the classic student struct but what good is having a student struct? You want a big database of student information so obviously it's gonna have to be a struct.
However for some reason it seems like it's almost impossible to find a tutorial video that goes through such a fundamental skill. Or even to find an example from google that uses them in that way (that seems very natural for solving real problems). Like sure it plays by the rules of passing struct and array as arguments, but it's really difficult to wrap your head first around structs and then trying to combine it to an array in syntax when you also pass it as an argument. Because it isn't as complicated as you'd assume, but you also would have to really understand what each part of the functions/methods/whatever mean. Like when do you have to call the general struct by name, when do you need to call the unique struct you created by name, when do you have to put a number to define or point to the array and when do you leave the brackets completely out.
It's very simple ONCE you see how it works, but it's almost impossible to find an example or figure it out by yourself. I'll give an example:
struct Student{ Let's not care about what's here};
Student createStudent(){just a function};
void printStudent(Student student[size], int count); // this one is a void function that takes the created student struct of Student that has the number of "size" members (of course it has to be static because it's an array in c++)
in main you would do
for (int i = 0; i < size; ++i) {
student1[i] = createStudent();
}
printStudent(student1, size);
Like this really messed me up with the struct "Student", the struct "Student" as the value of the "createStudent()" function, argument "Student student[size]", creating and defining "student1[i] = createStudent()" and then the argument "student1". If you really understand what each of those are from variables to structs to return values of function being just the Struct type to arguments having this or that information in definition but passed with no syntax, but creating it with syntax and having yet another name. The examples don't usually make it much easier when the repetitive naming is so common (like Student student and student1[i]) without explaining what each statement means and what is a type that you can use as a value and what is just a variable etc etc.
arrays become pointers when you pass them to functions, even if you declare the parameter type as an array
Hi, thxs foe your valuable content. I cant do a struct with func which is struct define with an array []
#include
#include
using namespace std;
//declaration of struct
struct PLAYER {
int id;
string name;
};
//func to enter the infos
void infos(PLAYER& x) {
for (auto c = 0; c < 3; ++c) {
cout > x.name;
x.id = c + 1; //enter the ID standart with auto increment
}
}
//search the name with an ID
int SELECTnameWHEREid(int index, PLAYER& x) {
for (auto& s : x) {
if (x.id == index) cout
That's because your parameters for your functions are for structs, and not for arrays. You need an array parameter.
PLAYER x[]
Would do it.
Also, quick tip: all uppercase characters are usually reserved for named constants, as in your first line of main. Usually, a struct will be named like this: Player