It is common to have either a pointer or a reference to a struct (or class). As you learned previously, you can select the member of a struct using the member selection operator (.):
1 2 3 4 5 6 7 8 9 |
struct Something { int nValue; float fValue; }; // Member selection using actual struct variable Something something; something.nValue = 5; |
This syntax also works for references:
1 2 3 |
// Member selection using reference to struct Something &ref = something; ref.nValue = 5; |
However, with a pointer, you need to dereference the pointer first:
1 2 3 |
// Member selection using pointer to struct Something *ptr = &something; (*ptr).nValue = 5; |
Note that the pointer dereference must be enclosed in parenthesis, because the member selection operator has a higher precedence than the dereference operator.
Because the syntax for access to structs and class members through a pointer is awkward, C++ offers a second member selection operator (->) for doing member selection from pointers. The following two lines are equivalent:
1 2 |
(*ptr).nValue = 5; ptr->nValue = 5; |
This is not only easier to type, but is also much less prone to error because the dereference is implicitly done for you, so there are no precedence issues to worry about. Consequently, when doing member access through a pointer, always use operator-> instead of operator.
![]() |
![]() |
![]() |
i love you Alex.
every time i stuck and want to understand some thing deeply,i just come here... and i am sure i will get what i want.
you must write all those heavy books.
not these stupids called writers that just make things hazier!
Brain overflow. Need to take a rest :)
What cannot be done using references in C++. Can we have array of references just like array of pointers?
I don't understand how
1.*pnValue = 6;
2.rnValue = 6;
if nValue started out as 5.
those are assignment statements that would change the value to 6 using either one.
just question :
is the code true ?
int returnLocal(){
int x=8 ;
return ++x;
}
int main() {
int a=returnLocal() ;
return 0;}
returnLocal() will return 9, which will be assigned to a. Then the program will exit.
I am not sure what you are asking.
thank you alex for evry think about this point .
I hope to be your friend.
sorry, i can not write english 100% , becouse i have mistakes.
thanks for all friends
Thanks for visiting!
Here is what I am trying to do:
Dynamically size an array
Pass a reference to that array to a function
Modify the size and or contents of the array
Return the reference to the modified array
I know this is possible using vectors, is it possible to do with arrays?
Thanks,
John
Yes, it is possible, just much more difficult than using vectors.
When you say "dynamically size an array", do you want to dynamically allocate an array to a particular size, or dynamically resize an existing array? Either thing is possible, but the latter is much more difficult.
Also, if you're passing a non-const reference to an array to a function, that means the function is going to modify the array. There's no need to return a reference back to the caller because the caller can access the array through it's original name.
How does one go about changing the size of an existing array? I guess you could just use c realloc, but surely there's a better way
You didn't end the struct.
[ That is one my very least favorite things about C/C++. It's so easy to forget, and then you get weird error messages that don't really indicate the problem. Thanks for letting me know. -Alex ]