For an assignment I came across this question.
What is the result of the statement following the definitions given below?
char c='a';
char *pc=&c;
char *&rc=pc ;
(*rc)++;
after printing all the different variables it shows that now variable c stores 'b'. I didn't understand why. Can anyone please explain?
Both char**
(pointer-to-pointer) and char*&
(reference-to-pointer) are second level abstractions of a value. (There's no pointer-to-reference type btw.)
The differences are really the same as they are for regular pointers vs. references: nullable vs. non-nullable etc. See here for more: https://www.geeksforgeeks.org/pointers-vs-references-cpp/
In your example, incrementing *rc
does the same as would with using *pc
.
Note that you don't need a symbol for accessing a references value as you would with *
when using a pointer.
Just for fun: Try char** ppc = &pc
. Can you tell what this does ?