Is this a correct statement ?
int & rnum = 12;
Is there any way to make this statement correct?
Printable View
Is this a correct statement ?
int & rnum = 12;
Is there any way to make this statement correct?
It is not a correct statement.
Even if you try to change it to [B]int &rnum=12;[/B]
it is not correct as you are declaring a variable with a reserved special character [B]&[/B].:eek:
I suppose you can add the keyword const i.e.
const int &rnum=12;
cuz 12 being a literal will be allocated as constant and so you can refer it using a constant reference.
yes,it will store within that adress of memory.
int &rnum=12; It will change the memory location of variable mum so that next time we store something to variable mum, it will be stored at the memory location 12 overwriting the already stored contents if any. Its not advisable because in normal case we dont know if any other program or variables are already using the memory location 12 and overwriting it will lead to system crash.. So its better not to change the memory location this way instead use the functions malloc() or calloc() and pointers for dynamically assigning & creating variables.
The operator '&' is mainly used to get the address of a variable so that we can assign it to another pointer variable and use both variables to access the contents in that address location.
int mum=10; int *pmum; pmum=&mum;
print mum; print *pmum; will give 10 10
*pmum=20;
print mum; print *pmum; will give 20 20
mum=30;
print mum; print *pmum; will give 30 30
So, '&' is mainly used for only reading the address of variable and not to assign or change the address of variable even if its possible.
it represents the address operator we can't initialize the memory address to the variable or a value of address to the pointer variable too we can give like this
int *&num=12;
[QUOTE=sutnarcha;9900]It is not a correct statement.
Even if you try to change it to [B]int &rnum=12;[/B]
it is not correct as you are declaring a variable with a reserved special character [B]&[/B].:eek:[/QUOTE]
it wont be correct.as there is a special character "&".
HI,
All of you
can you please tell me how can i get interview questions of networking and linux on my mail
[QUOTE=anoop4real;9892]Is this a correct statement ?
int & rnum = 12;
Is there any way to make this statement correct?[/QUOTE]
the correct statement is -
int rnum=12;
[QUOTE=anoop4real;9892]Is this a correct statement ?
int & rnum = 12;
Is there any way to make this statement correct?[/QUOTE]
In C/C++ this is not a valid statement.
To make it a valid C++ statement add "const" in the statement like this.
const & rnum = 12;
Hello All,
Actually the compiler doesnt allow the user to allocate value at the desired location.
int &rnum=12, its trying to assign address 12 to rnum which is not allowed.
Couple of answers said we can declare using const & rnum = 12 and int *&num=12;
but doesn't work. I checked it on the GCC version 4.0.1.
I think the only way we can do it is to have pointer and assign the address for it.
regards
Prakash
yes prakash is right we cannot change address of a variable even by using const......... so its a wrong statement
Not at all ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Not at all !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!