“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
May be this is the reason
Array of reference is not possible Bcoz array of pointers is not possible.
array of pointers is not possible bcoz reference may refer to any other variable
array is to store variable, but reference is not a type of variable but only the address of variable.
You can’t assign value to a reference like:
int a = 8, b= 9; & a = &b ;
nor
&a= 0X0000….;
nor
1) The reference must be initialized by the time it is declared e.g. int &r = a;
2) But it is also possible to have something like
int p[5] = {2,3,4} which would mean that the last two elements be initialized to a default ‘0′.
Going by the argument #2), we would end up something like int&r[5] = {a, b, c} assuming a,b,c are integers — which would mean that the last two references in the array not to refer to any valid memory location that is against argument#1.
array of reference is not possible because we cant change the address of reference until u initialize it for the next data type..that means we cant access elements as in normal aaray…
but if you create an array of references then the values
you initialise may not have contiguous values in memory.
Hence C doesnot hv array of refernces.
But it is theoraticaly possible to have that facility in the language definition.
Can we treat an address as an value, i mean, consider 000f(example) and storing this value in an array element, after retrievin we can treat it as an address.
Reference variable must be initializd when it is created. This not possible with array of references.
reference is actually an alias name given to an existing memory location.
eg:
int i=10;
int &j=i;
printf(”%u %u”,&i,&j);
This will print the same address for i & j.
arrays are already passed by refrence not by value
Because reference variables must be initialized in C++, but there is no way to initialize arrays!
May be this is the reason
Array of reference is not possible Bcoz array of pointers is not possible.
array of pointers is not possible bcoz reference may refer to any other variable
array is to store variable, but reference is not a type of variable but only the address of variable.
You can’t assign value to a reference like:
int a = 8, b= 9; & a = &b ;
nor
&a= 0X0000….;
nor
int &A [2]= {&a,&b};
Instead, you can use pointer:
int * A[2] = {&a,&b};// pointer is a Variable!
1) The reference must be initialized by the time it is declared e.g. int &r = a;
2) But it is also possible to have something like
int p[5] = {2,3,4} which would mean that the last two elements be initialized to a default ‘0′.
Going by the argument #2), we would end up something like int&r[5] = {a, b, c} assuming a,b,c are integers — which would mean that the last two references in the array not to refer to any valid memory location that is against argument#1.
array of reference is not possible because we cant change the address of reference until u initialize it for the next data type..that means we cant access elements as in normal aaray…
an array has contiguous elements.
but if you create an array of references then the values
you initialise may not have contiguous values in memory.
Hence C doesnot hv array of refernces.
But it is theoraticaly possible to have that facility in the language definition.
Can we treat an address as an value, i mean, consider 000f(example) and storing this value in an array element, after retrievin we can treat it as an address.
Reference variable must be initializd when it is created. This not possible with array of references.
reference is actually an alias name given to an existing memory location.
eg:
int i=10;
int &j=i;
printf(”%u %u”,&i,&j);
This will print the same address for i & j.
reference is only compiling-time alias of some memory address. It is not a pointer. Only the second name of some variable.
Defining the reference does not increase memory usage.
When you create an array, you create an array of some data, that has specific size and is contiguous in memory with sequenced memory addressed.
Pointer is a variable, that takes some memory to store data address, reference is only alias, used during compile time.
I am just wondering why can’t we have:
int&r[5] = {a, b, c, d, e}
where a, b, c, d, e are integers.
Isn’t this considered as an array of references?
no, a reference is not an object,,,,so we cant find the address…of reference
we can do …references to an array…
int z[]={1,2,3,4,5};
int (&s)[5]=z;
Leave an Answer/Comment