66
asked on
Reference Variable
Hello freinds,
my doubt is why we can't declare a reference variable for a void pointer.
Please clarify my doubt.Thanks in advance.
my doubt is why we can't declare a reference variable for a void pointer.
Please clarify my doubt.Thanks in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, unlike a pointer, a reference must always point to the same thing. You can change a pointer to point to different things. But a reference cannot be made to point to (refer to) different things, for example, you can do
int i;
int j;
int *IPtr = &i;
IPtr = &j; // Make pointer point to other int.
Byut if you do.
int i;
int j;
int IRef = i; // IRef refers to forever.
IRef = j; // This changes the value in what IRef refers to (i). IRef still refers to i.
you don't change IRef to refer to something else. It still refers to i, but now i had a new value. Thus, there is less reason to have NULL references since you can't start out with a NULL reference and then change it to something else later.
int i;
int j;
int *IPtr = &i;
IPtr = &j; // Make pointer point to other int.
Byut if you do.
int i;
int j;
int IRef = i; // IRef refers to forever.
IRef = j; // This changes the value in what IRef refers to (i). IRef still refers to i.
you don't change IRef to refer to something else. It still refers to i, but now i had a new value. Thus, there is less reason to have NULL references since you can't start out with a NULL reference and then change it to something else later.
ASKER