Prepare for your Next Interview
This is a discussion on Is this a correct statement within the C and C++ forums, part of the Software Development category; Is this a correct statement ? int & rnum = 12; Is there any way to make this statement correct?...
|
|||
|
Re: Is this a correct statement
It is not a correct statement.
Even if you try to change it to int &rnum=12; it is not correct as you are declaring a variable with a reserved special character &. ![]()
__________________
Lack of WILL POWER has caused more failure than lack of INTELLIGENCE or ABILITY. -sutnarcha- |
|
|||
|
Re: Is this a correct statement
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. |
|
|||
|
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.
__________________
Regards, Anoop :) If its useful, dont forget to [COLOR="Red"]THANK[/COLOR] me :cool: |
|
|||
|
Re: Is this a correct statement
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; |
|
|||
|
Re: Is this a correct statement
it wont be correct.as there is a special character "&".
|
|
|||
|
Re: Is this a correct statement
Quote:
int rnum=12; |
|
|||
|
Re: Is this a correct statement
Quote:
To make it a valid C++ statement add "const" in the statement like this. const & rnum = 12; |
|
|||
|
Re: Is this a correct statement
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 |
| The Following User Says Thank You to prakash.kudrekar For This Useful Post: | ||
|
|||
|
Not at all ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Last edited by aman15490 : 05-22-2008 at 06:23 AM. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C/C++ Programming : Limitations of switch statement | Lokesh M | C and C++ | 1 | 07-17-2007 02:38 AM |
| How can I verify rediffmail page is correct one or not | Geek_Guest | WinRunner | 4 | 06-01-2007 06:42 AM |
| How can i know that connected page is correct or not? | Geek_Guest | WinRunner | 2 | 05-18-2007 02:35 AM |
| find the correct switch..... | psuresh1982 | Brainteasers | 7 | 02-08-2007 03:57 AM |
| Rules for COPY statement | norman | MainFrame | 1 | 08-14-2006 04:11 AM |