Why is it required to initialise the references during its initialisation. If not initialised it throws errors. Is it const datas should always be initialised with a value. Why is it only with constants and does not require a variable to be initialised during declaration.
EDITED:
#include<stdio.h>
int main()
{
int a=10,b=12;
int &c=a;
printf("%d\n",a);
c=b;
printf("%d\n",a);
printf("%d\n",c);
return 0;
}
This is a c program where I used reference. Am I wrong in this?
The code you posted can’t be compiled as a C program, since there are no references in C.
To make it compile as C, you would have to modify it:
As for C++ references, the fact that a reference needs to be initialized is one of the major differences between pointers and references.
Pointers can be NULL or invalid, references can’t (actually, you can generate an invalid reference, but the compiler should warn you about that). In a way, a reference works as an alias. By writing
int a; int &b=a;, you now have two names for one variable (or memory location).