Passing Structure to Functions by Value, Pointer (Address) | C++ Video Tutorial

Поделиться
HTML-код
  • Опубликовано: 19 окт 2024
  • In this c++ / cpp programming video tutorials / lecture for beginners video series, you will learn how to pass structures to functions by value and reference with example.
    You will learn how to pass a structure to a function by value, how to access the members when they are passed to a function by value, what happens when we change the value of a structure when it is passed by value, how to pass the address of a variable to a function, how to pass it by reference, how to access the members when they are passed by reference, what happens when we change the value of a structure member w in detail with example.
    Visit www.LearningLad... to get the SOURCE CODE of this tutorial and to watch more free computer programming video tutorials.
    Learn Programming in HINDI at our youtube channel
    / learningladhindi
    Catch us on SocialMedia
    / learninglad
    www.google.com/...
    / learningladedu

Комментарии • 93

  • @nishaphd
    @nishaphd 7 лет назад +17

    superb explanation for call by value and call by reference. I have studied engg. main stream was IT. if I were got a tutor like you in my college I would have go to IT field. Thanks anil sir.keep rocking...

    • @LearningLad
      @LearningLad  7 лет назад +3

      Thank you.
      I'm really glad that my tutorials are helping you.
      Keep Learning and Supporting :)

    • @vendetta3953
      @vendetta3953 5 лет назад

      NishaReetha, God saved you XD

  • @ksun8993
    @ksun8993 5 лет назад +10

    Sir, i got a clear understanding of passing structures to functions.....before that i had lots of confusion....now everthing is cleared.... thankyou sir!

  • @0231-g6z
    @0231-g6z 7 лет назад +4

    This guy is a beast of an instructor. thanks!!

  • @rogerhunter313
    @rogerhunter313 3 года назад +1

    I cannot thank you enough, Anil. This was a great tutorial.

  • @katrinejensrud537
    @katrinejensrud537 4 года назад +2

    thank you so much for your good explanation

  • @shamlanalmusallam1504
    @shamlanalmusallam1504 3 года назад +1

    U are a real one. Thank you

  • @mrnano1991
    @mrnano1991 8 лет назад +4

    Anil, thank you!!
    I have a question why the value didn't change when we applied the change on the first function?

    • @sulaimanjohnny6529
      @sulaimanjohnny6529 5 лет назад +9

      Because the first function only created a copy of the value since we passed by value. The second function we created was a pointer so we passed by address. in other words, we passed the structs memory address to the function so when we changed the dereferenced pointer it already had the structs memory address and changed the memory address to the new variable he entered which is also called pass by address. Also if you're wondering why the console printed out 0 and not 0000 is because it was a type int var. I know am like over 2 years late but oh well if someone stumbles across this comment hope this helps them :)

    • @akshay8675
      @akshay8675 5 лет назад

      @sulaiman johny ty

  • @bendhimanish
    @bendhimanish 9 лет назад +2

    hi anil whats the difference between passing using the address and directly and how did the values change when you passed it using the address using the pointers

    • @LearningLad
      @LearningLad  9 лет назад +7

      Manish Reddy
      consider
      void display(int y){
      ...
      }
      void show(int *p){
      ....
      }
      int main(){
      ...
      int x = 10;
      display(x);
      show(&x);
      ...
      }
      when you pass by value, in this example with display() function, the value of variable x (i.e 10) will be copied to variable y. x and y are different variables and if you change the value of y in display unction, it will not affect the value of x in main function.
      when you pass by reference, in this example with show() function, the address of variable x is copied to pointer p.
      So now pointer p is pointing to variable x and that's why if you change the value at the memory location pointed by pointer p in show function, then that change will also reflect with variable x in main function.
      i have explained about pass by reference in this video.
      ruclips.net/video/xWItyeMpagE/видео.html
      check it out for more info!
      Hope this helps :)

    • @bendhimanish
      @bendhimanish 9 лет назад +3

      ***** thanx a lot man

  • @eduardoandrescontrerasrome6703
    @eduardoandrescontrerasrome6703 3 года назад +1

    YOU ARE THE BEST THANKS!

  • @yosrimhamdi1435
    @yosrimhamdi1435 7 лет назад +1

    hello Anil,
    please tel me why when I pass an array into a function and I make some modification in its values so this array will be
    immediately reflected is the main function, otherwise as you told us changing a structured variable's value which have been passed by value in a function does not take any effect in the main function, any explanation please?

  • @subhodeepsinha7890
    @subhodeepsinha7890 6 лет назад +1

    Sir...Why does the vale roll=0000 changes in case of pointer and not for dot operator?

  • @ShubhamKumar-et7gx
    @ShubhamKumar-et7gx 3 года назад +1

    but how value change in void display it should change in in void show na..as it it contain pointer variable??please clarify it

  • @prakashsaini5810
    @prakashsaini5810 6 лет назад

    Thanks sir for these tutorials .
    But I want to ask wht was your reference for all these tutorials

  • @mdazizulislam2425
    @mdazizulislam2425 7 лет назад

    Thanks for clear explanation.

  • @raiyanrahman79
    @raiyanrahman79 6 лет назад +1

    SIR I have a question.
    Why didn't you use value it operator '*' why the pointers like this :
    cout

  • @CalistheNick01
    @CalistheNick01 5 лет назад

    Just what I needed

  • @TheSuperMisterious98
    @TheSuperMisterious98 6 лет назад

    if i want to pass a struct as parameter of a function in an header file? do i have to declare te struct type in the header also?

  • @GuyBoazy
    @GuyBoazy 9 лет назад +3

    Thank you!! great tut :)

  • @anthonylhoward
    @anthonylhoward 8 лет назад

    Thanks man. Reviewing for class

  • @zehraarshadmulla9976
    @zehraarshadmulla9976 3 года назад

    if i have a nested structure then how to define its value, do i use double curly bracket ?

  • @diptishramteke9427
    @diptishramteke9427 8 лет назад +1

    Why do only changing value in pointer function changes the value ?? why not when directly accessing by value function??

  • @corey7542
    @corey7542 7 лет назад +2

    aaand the next thing i want you guys to teach is :D.. lol great teaching tho

  • @smrtfasizmu6161
    @smrtfasizmu6161 2 года назад

    I like that you call pointer adress several times in the video, when writing/reading code I often read pointer as adress. Also, it is weird that you can pass by value at all in C/C++. In other languages that I have seen you can only ever pass the memory adresses of instances of classes.

  • @bredmond812
    @bredmond812 5 лет назад

    So can i say in general that using normal variables are "pass by reference", and pointer variables to "pass by value"?

    • @bredmond812
      @bredmond812 5 лет назад

      @Peterolen thank you very much!

  • @krishnilbhojani3343
    @krishnilbhojani3343 5 лет назад +1

    Good explanation sir...

  • @subhamjain4071
    @subhamjain4071 7 лет назад +1

    loved ur explanation...nice one

  • @jonnyschmidt5876
    @jonnyschmidt5876 10 лет назад +2

    Thank you soooo much

  • @paulcabrera5895
    @paulcabrera5895 3 года назад

    can you make a video explaining it with a struct of arrays and how to pass everything by reference thought functions

  • @MrFiremarshall007
    @MrFiremarshall007 8 лет назад

    very helpful, thanks

  • @khanh6903
    @khanh6903 8 лет назад +1

    Thank you!

    • @LearningLad
      @LearningLad  8 лет назад

      +Kevin “Khanh” Do
      pleasure :)

  • @nidhicpatel5689
    @nidhicpatel5689 6 лет назад

    can we use 'anil' as variable in 'display ' function instead of 's' as shown in your vedio??

  • @Impulse_Photography
    @Impulse_Photography 6 лет назад

    it is possible for me to pass directly into a function not just the object but its members. Not just 'S' but s.name or s.sex directly into the function???

  • @muhammadaeidel
    @muhammadaeidel 3 года назад +1

    What IDE is this? Window or mac?

    • @LearningLad
      @LearningLad  3 года назад

      codeblocks ide used ( windows )

  • @aparnamane1164
    @aparnamane1164 7 лет назад +1

    Why does the value of roll number does not change when changes are made in void display (pass by value) function

    • @Impulse_Photography
      @Impulse_Photography 6 лет назад +1

      becase when you call By-Valuie it makes a copy and sends that into the function. You are now making changes to the copy not the original back in the Main(). A pointer, points directly to the memory address of the original

  • @yenamalasusmitha7244
    @yenamalasusmitha7244 8 лет назад

    hi sir, I have a doubt here, here we are passing the structure variables to the function not the whole structure right, pls correct me if I am wrong

    • @nirmalraj4167
      @nirmalraj4167 7 лет назад

      we are passing structure variable called anil from that variable we are accessing all members like rollNo, age and sex. not passing the whole structure

  • @paulaolopes8264
    @paulaolopes8264 6 лет назад

    In main function, how could pass the parameters to variable anail, without creating a constructor?

  • @pepejoong5272
    @pepejoong5272 6 лет назад +1

    Thank you sir

  • @palwan7190
    @palwan7190 6 лет назад

    can annyone reccommend which ide highlights syntax error except visual studio

    • @palwan7190
      @palwan7190 5 лет назад

      ​@Peterolen visual studio highlights syntax error the moment you write them wrong others show it after building or compiling the code

  • @soumyashreerajalabandi3718
    @soumyashreerajalabandi3718 6 лет назад

    Even though u pasted show function before display the output was same as before. Only the rollno value was changed by passing the reference

  • @_abhishek.verma_
    @_abhishek.verma_ 5 лет назад +1

    I have a doubt, why are we passing &anil instead of anil in case of pointer

    • @aswathsuresh5330
      @aswathsuresh5330 5 лет назад +1

      Because the argument of the function show is a pointer. So you are passing the address of anil object using &anil for it to work.

  • @roh9934
    @roh9934 7 лет назад

    how can we use struct datatype to return an array from a function and something like that
    For Example: struct Rohit{int age;
    };
    and then struct rohit r;
    r myfunc()
    {
    return something;
    }
    Please Help

  • @muhdhafiz6804
    @muhdhafiz6804 7 лет назад

    i have a struct. Linked list. How do i display information stored into a file?
    Please reply.

  • @midnightphantom4787
    @midnightphantom4787 4 месяца назад

    Man this thing is not working for me my progam considers function as a variable just cuz its void

  • @Makendu
    @Makendu 8 лет назад

    instead of just doing void how do you return a value

  • @ritech2877
    @ritech2877 6 лет назад +1

    How can we pass a structure containing the values of two or more students?

    • @LearningLad
      @LearningLad  6 лет назад

      yes you can.
      just pass an array of structure.

    • @ritech2877
      @ritech2877 6 лет назад +1

      Ok..thanku so much😊

  • @zoranjovanov8564
    @zoranjovanov8564 8 лет назад +2

    nope. adress != reference
    reference creates an alias of a value and its working the alias not the actual value. rename ur video pass by value and pass by pointer( adress )

  • @pvm1936
    @pvm1936 8 лет назад

    why is it not printing 0000 but 0?

  • @rodwynnejones
    @rodwynnejones 6 лет назад

    interesting:
    You used "&anil" when calling the show function rather than doing:-
    student *anilptr;
    anilptr = &anil;
    show(anilptr);
    Both work as intended.

  • @ambersingh9408
    @ambersingh9408 7 лет назад

    But case of array is different .In array only pass by reference exists no pass by value

  • @kishorsubedi4215
    @kishorsubedi4215 7 лет назад

    I think it's members, not parameters of the struct. Parameters are for functions, I guess. ;)
    Anyways, thumbs up for the tutorial

  • @saikiranrudra1283
    @saikiranrudra1283 7 лет назад +1

    why 0 is printed why not 0000 is printed ?

    • @rodwynnejones
      @rodwynnejones 6 лет назад +2

      Because in this case, "rollno" was declares as an "int" (a number) so '0000' is just '0'. If you need to keep the output format '0000', just declare "rollno" as a string and assign using double quotes like you do for any other string.

  • @mica122213
    @mica122213 10 лет назад +1

    anil not working unless i do student()
    anil.rollno
    anil.sex
    etc.
    whenver i do student {1234,'m', etc}gets error.

    • @nirmalraj4167
      @nirmalraj4167 7 лет назад

      student = {1234, 'm', etc} will work

  • @ritech2877
    @ritech2877 6 лет назад

    Sir please give an example of passing array of structure..i am not able to do it..please..very small example..please..😕
    Edit: sir please...i have tried a lot..i am not able to do it..sir please...sir..i am not able to jump to.the next video..i want to do it sir please..😯

  • @odotk
    @odotk 5 лет назад

    0:36 .... carsex the highlight oh the video

  • @batman_1st
    @batman_1st 7 лет назад +4

    Why he keep calling 'm' as 'yum' and 's' as 'yes'?

    • @krishnilbhojani3343
      @krishnilbhojani3343 5 лет назад +1

      If you have problem, then why are you watching him??
      He is atleast helping us bro..

    • @akshay8675
      @akshay8675 5 лет назад

      @Batman1st Doodle Jump,,,,get a life bitch....(this is the first time i call someone a 'bitch' in yt....BT U deserve IT MAHN)

  • @teamtamilasfc3128
    @teamtamilasfc3128 3 года назад

    Anil❤️😘

  • @SabirDesign
    @SabirDesign 7 лет назад +1

    nothing understand.....
    say m . you say aaaaam

    • @nishaphd
      @nishaphd 7 лет назад +2

      WizCanva if you can't understand means its your problem.. its not about the tutor.. either school or college, teachers taught to all students. some of them got good marks , some of them got failed. it means the inability of the student that could not get good marks. not about inability of the tutor.

    • @junaidengineerings
      @junaidengineerings 6 лет назад

      idda tu

  • @medinasFamily
    @medinasFamily 6 лет назад

    This is not how you pass a pointer to a function. First you must declare a pointer variable and then point it to another variable. Then pass the pointer to the function. What you did is passed by reference to a function that has a pointer parameter that points to a Student data type. Not the same thing.

  • @spacewardDev
    @spacewardDev 8 лет назад

    billd an run dis

  • @priyachaudhary3518
    @priyachaudhary3518 5 лет назад

    sex is not a correct word to use