Passing structures to functions -- C++ Structs Tutorial #6

Поділитися
Вставка
  • Опубліковано 30 вер 2024
  • Learn all the ways you can pass structures to functions as arguments: passing structure members, passing structures by value, and passing structures by reference.
    In this C++ programming tutorial for beginners, you'll learn through coding examples using Visual Studio Community.
    // Learn More
    Structures Playlist ⇒ • CH11: Structures
    C++ Programming for Everyone ⇒ • Playlist
    // Consider supporting this channel in multiple ways
    paypal.me/hank...
    Bitcoin: 177wfkQwzXiC8o2whQMVpSyuWUs95krKYB
    Dogecoin: DRK2HDp3ZkkbFpvGZnVgMVosnBhPv8r3uP

КОМЕНТАРІ • 13

  • @Yupppi
    @Yupppi 11 місяців тому

    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.

  • @Yupppi
    @Yupppi 11 місяців тому

    I found out that even with Struct& reference the original struct defined in main() didn't change for some reason.

  • @kemalali5683
    @kemalali5683 Рік тому

    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

    • @ProfessorHankStalica
      @ProfessorHankStalica  Рік тому

      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

  • @qcnck2776
    @qcnck2776 Рік тому +1

    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!

  • @Fuzzybunny-yf2lh
    @Fuzzybunny-yf2lh Рік тому

    You are legitimately a GODSEND. This explanation cleared up all my confusion about passing structs by reference. Keep up the phenomenal work!! :)

  • @TheTimeProphet
    @TheTimeProphet 10 місяців тому

    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.

    • @ProfessorHankStalica
      @ProfessorHankStalica  10 місяців тому +1

      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!

  • @OmarMubarak-cn3fo
    @OmarMubarak-cn3fo 5 місяців тому

    Thank you so much !

  • @ianneill9188
    @ianneill9188 Рік тому

    Super explanation! Thank you.