Results 1 to 14 of 14

Thread: Is this a correct statement

  1. #1
    Junior Member
    Join Date
    Mar 2007
    Answers
    2

    Is this a correct statement

    Is this a correct statement ?
    int & rnum = 12;
    Is there any way to make this statement correct?


  2. #2
    Expert Member
    Join Date
    Nov 2006
    Answers
    518

    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-

  3. #3
    Junior Member
    Join Date
    Jun 2007
    Answers
    4

    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.


  4. #4
    Junior Member
    Join Date
    Jul 2007
    Answers
    1

    Re: Is this a correct statement

    yes,it will store within that adress of memory.


  5. #5
    Contributing Member
    Join Date
    Apr 2007
    Answers
    58

    Smile Re: Is this a correct statement

    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:

  6. #6
    Junior Member
    Join Date
    Feb 2007
    Answers
    7

    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;


  7. #7
    Junior Member
    Join Date
    Jul 2007
    Answers
    4

    Re: Is this a correct statement

    Quote Originally Posted by sutnarcha View Post
    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 &.
    it wont be correct.as there is a special character "&".


  8. #8
    Junior Member
    Join Date
    May 2006
    Answers
    1

    Re: Is this a correct statement

    HI,

    All of you
    can you please tell me how can i get interview questions of networking and linux on my mail


  9. #9
    Junior Member
    Join Date
    Jul 2007
    Answers
    1

    Re: Is this a correct statement

    Quote Originally Posted by anoop4real View Post
    Is this a correct statement ?
    int & rnum = 12;
    Is there any way to make this statement correct?
    the correct statement is -
    int rnum=12;


  10. #10
    Junior Member
    Join Date
    Jul 2007
    Answers
    2

    Re: Is this a correct statement

    Quote Originally Posted by anoop4real View Post
    Is this a correct statement ?
    int & rnum = 12;
    Is there any way to make this statement correct?
    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;


  11. #11
    Contributing Member
    Join Date
    Apr 2007
    Answers
    41

    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


  12. #12
    Junior Member
    Join Date
    May 2008
    Answers
    4

    Re: Is this a correct statement

    yes prakash is right we cannot change address of a variable even by using const......... so its a wrong statement


  13. #13
    Expert Member
    Join Date
    May 2008
    Answers
    100

    Thumbs up Re: Is this a correct statement

    Not at all ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Last edited by aman15490; 05-22-2008 at 06:23 AM.

  14. #14
    Expert Member
    Join Date
    May 2008
    Answers
    100

    Thumbs up Re: Is this a correct statement

    Not at all !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact