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
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...
Thank you.
I'm really glad that my tutorials are helping you.
Keep Learning and Supporting :)
NishaReetha, God saved you XD
Sir, i got a clear understanding of passing structures to functions.....before that i had lots of confusion....now everthing is cleared.... thankyou sir!
my pleasure.
keep learning :)
This guy is a beast of an instructor. thanks!!
pleasure :)
I cannot thank you enough, Anil. This was a great tutorial.
You are most welcome
thank you so much for your good explanation
Glad it was helpful!
U are a real one. Thank you
Pleasure
Anil, thank you!!
I have a question why the value didn't change when we applied the change on the first function?
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 :)
@sulaiman johny ty
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
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 :)
***** thanx a lot man
YOU ARE THE BEST THANKS!
pleasure :)
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?
Sir...Why does the vale roll=0000 changes in case of pointer and not for dot operator?
but how value change in void display it should change in in void show na..as it it contain pointer variable??please clarify it
Thanks sir for these tutorials .
But I want to ask wht was your reference for all these tutorials
Thanks for clear explanation.
SIR I have a question.
Why didn't you use value it operator '*' why the pointers like this :
cout
you can use cout
Just what I needed
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?
Thank you!! great tut :)
Guy Boazy
thanks man :)
Thanks man. Reviewing for class
if i have a nested structure then how to define its value, do i use double curly bracket ?
Why do only changing value in pointer function changes the value ?? why not when directly accessing by value function??
aaand the next thing i want you guys to teach is :D.. lol great teaching tho
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.
So can i say in general that using normal variables are "pass by reference", and pointer variables to "pass by value"?
@Peterolen thank you very much!
Good explanation sir...
thank you :)
loved ur explanation...nice one
Thank you :)
Thank you soooo much
can you make a video explaining it with a struct of arrays and how to pass everything by reference thought functions
very helpful, thanks
Thank you!
+Kevin “Khanh” Do
pleasure :)
can we use 'anil' as variable in 'display ' function instead of 's' as shown in your vedio??
can u ans my que plz....
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???
What IDE is this? Window or mac?
codeblocks ide used ( windows )
Why does the value of roll number does not change when changes are made in void display (pass by value) function
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
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
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
In main function, how could pass the parameters to variable anail, without creating a constructor?
Thank you sir
my pleasure.
keep learning :)
can annyone reccommend which ide highlights syntax error except visual studio
@Peterolen visual studio highlights syntax error the moment you write them wrong others show it after building or compiling the code
Even though u pasted show function before display the output was same as before. Only the rollno value was changed by passing the reference
I have a doubt, why are we passing &anil instead of anil in case of pointer
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.
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
i have a struct. Linked list. How do i display information stored into a file?
Please reply.
Man this thing is not working for me my progam considers function as a variable just cuz its void
instead of just doing void how do you return a value
How can we pass a structure containing the values of two or more students?
yes you can.
just pass an array of structure.
Ok..thanku so much😊
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 )
why is it not printing 0000 but 0?
interesting:
You used "&anil" when calling the show function rather than doing:-
student *anilptr;
anilptr = &anil;
show(anilptr);
Both work as intended.
But case of array is different .In array only pass by reference exists no pass by value
I think it's members, not parameters of the struct. Parameters are for functions, I guess. ;)
Anyways, thumbs up for the tutorial
why 0 is printed why not 0000 is printed ?
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.
anil not working unless i do student()
anil.rollno
anil.sex
etc.
whenver i do student {1234,'m', etc}gets error.
student = {1234, 'm', etc} will work
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..😯
0:36 .... carsex the highlight oh the video
Why he keep calling 'm' as 'yum' and 's' as 'yes'?
If you have problem, then why are you watching him??
He is atleast helping us bro..
@Batman1st Doodle Jump,,,,get a life bitch....(this is the first time i call someone a 'bitch' in yt....BT U deserve IT MAHN)
Anil❤️😘
nothing understand.....
say m . you say aaaaam
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.
idda tu
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.
billd an run dis
sex is not a correct word to use