Oh, I forgot to print it out in main. So, if we successfully did it and wanted to print out the value of the changed price (the main local variables will also change because we used a pointer as a dummy argument in the function), we would do (in C++):
cout << "The price of Apple had been changed to: " << Price.Apple;
cout << "The price of Banana had been changed to: " << Price.Banana;
The values printed out in console would be as follows:
And yes, my question is still, 'why' don't we need to? I assume this different to arrays where the name of the array contained the address of the start of the element and so hence we did not need to use the REFERENCE operator, but with in this case, the DEFERENCE operator. Is this correct?
Yes, as you can see, pointers and such that when used, really do confuse me.
@itsdannyftw Answer to the question "why don't we need to?" is that this is in the way -> operator is defined. When you have a pointer to structure you can access its members using -> operator. If you have the structure itself then you use . (dot) to access the members. Now you can also write something like (*pRect).length instead of rect->length and that will work too. Why? Because *pRect represents the structure itself, and hence you can access its members with a . (dot). Cool huh!
Hence the "->" connotation conveniently removes the rule usage of using the deference operator? Or am I misunderstanding this entire logic? I assume it's the same concept along with when assigning values to the member variables using functions as well!
Since the "->" is a connotation to be used when you want a pointer to access a member variable of a structure, does this mean that you do not have to use the deference operator in advance? So, you do not have to (or you're not even supposed to???) do this:
result = *rect->length * *rect->width
Basically, my logic is that when you want to access a value (or a member variable (in this case, length & width), that is being pointed by a pointer, you would have to use the deference operator.
@arghyainfo Thanks for liking the videos. I really appreciate it. Looks like you have already watched all of them. I haven't posted this one on facebook yet. I have been making about 6 to 8 tutorials per week. Subscribe to the channel and you will be able to access my new videos in your Subscription section.
Oh, I forgot to print it out in main. So, if we successfully did it and wanted to print out the value of the changed price (the main local variables will also change because we used a pointer as a dummy argument in the function), we would do (in C++):
cout << "The price of Apple had been changed to: " << Price.Apple;
cout << "The price of Banana had been changed to: " << Price.Banana;
The values printed out in console would be as follows:
8
16
This should be correct, hopefully gets cleared up
itsdannyftw 1 year ago
And yes, my question is still, 'why' don't we need to? I assume this different to arrays where the name of the array contained the address of the start of the element and so hence we did not need to use the REFERENCE operator, but with in this case, the DEFERENCE operator. Is this correct?
Yes, as you can see, pointers and such that when used, really do confuse me.
itsdannyftw 1 year ago
@itsdannyftw Answer to the question "why don't we need to?" is that this is in the way -> operator is defined. When you have a pointer to structure you can access its members using -> operator. If you have the structure itself then you use . (dot) to access the members. Now you can also write something like (*pRect).length instead of rect->length and that will work too. Why? Because *pRect represents the structure itself, and hence you can access its members with a . (dot). Cool huh!
Learnorama 1 year ago
@Learnorama
Ah, very cool! Just wondering though, why do you need the brackets around it? Why couldn't you do *pRect.Length?
itsdannyftw 1 year ago
@itsdannyftw
You can't do *pRect.Length because of operator precedence. The "." operator has higher precedence than " * ".
To avoid compile errors you would have to use explicit parentheses (*pRect).Length
ronswayoflife 2 months ago
@ronswayoflife
Cheers bro!! After 1 year, I would sure love to get back into programming!! I'm so ponderous at this stuff!
itsdannyftw 2 months ago
Ok, lets just create a function that changes the value of those structure member variables.
void Change(struct Price_Struct *price)
{ price->Apple = 8; price->Banana = 16;
}
So this is fine? Hence, no deference operator had to be used?
I'll continue further in the next comment.
itsdannyftw 1 year ago
@itsdannyftw That's correct.
Learnorama 1 year ago
Hence the "->" connotation conveniently removes the rule usage of using the deference operator? Or am I misunderstanding this entire logic? I assume it's the same concept along with when assigning values to the member variables using functions as well!
So, lets say we have a structure:
struct Price_Struct {
int Apple = 2;
int Banana = 4;
}
In main, we declare it as: Price_Struct Price
I will continue this in the next comment.
itsdannyftw 1 year ago
Since the "->" is a connotation to be used when you want a pointer to access a member variable of a structure, does this mean that you do not have to use the deference operator in advance? So, you do not have to (or you're not even supposed to???) do this:
result = *rect->length * *rect->width
Basically, my logic is that when you want to access a value (or a member variable (in this case, length & width), that is being pointed by a pointer, you would have to use the deference operator.
itsdannyftw 1 year ago
Thank you for the tutorial, as the tips are helpful and percise.
Tod Burkett
08/23/2010 12:29 p.m.
Todplunkt 1 year ago
more videos plz
arghyainfo 1 year ago
@arghyainfo Coming up ;-)
Learnorama 1 year ago
this are all nice videos.thank u to help us .....
give me more videos to learn fast.
arghyainfo 1 year ago
@arghyainfo Thanks for liking the videos. I really appreciate it. Looks like you have already watched all of them. I haven't posted this one on facebook yet. I have been making about 6 to 8 tutorials per week. Subscribe to the channel and you will be able to access my new videos in your Subscription section.
Learnorama 1 year ago