Geeks Talk

Prepare for your Next Interview




Is this a correct statement

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?...


Go Back   Geeks Talk > Software Development > C and C++

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 04-05-2007
Junior Member
 
Join Date: Mar 2007
Location: India
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
anoop4real is on a distinguished road
Is this a correct statement

Is this a correct statement ?
int & rnum = 12;
Is there any way to make this statement correct?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-06-2007
Expert Member
 
Join Date: Nov 2006
Location: Hyd-IND
Posts: 508
Thanks: 1
Thanked 52 Times in 44 Posts
sutnarcha is on a distinguished road
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-
Reply With Quote
  #3 (permalink)  
Old 06-27-2007
Junior Member
 
Join Date: Jun 2007
Location: Delhi, India
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
sandeep6883 is on a distinguished road
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.
Reply With Quote
  #4 (permalink)  
Old 07-02-2007
Junior Member
 
Join Date: Jul 2007
Location: secunderabad
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
srinugrandhi is on a distinguished road
Re: Is this a correct statement

yes,it will store within that adress of memory.
Reply With Quote
  #5 (permalink)  
Old 07-02-2007
Contributing Member
 
Join Date: Apr 2007
Location: India
Posts: 61
Thanks: 2
Thanked 13 Times in 9 Posts
Haitalk is on a distinguished road
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:
Reply With Quote
  #6 (permalink)  
Old 07-12-2007
Junior Member
 
Join Date: Feb 2007
Location: india
Posts: 10
Thanks: 2
Thanked 3 Times in 2 Posts
sundarkms is on a distinguished road
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;
Reply With Quote
  #7 (permalink)  
Old 07-28-2007
Junior Member
 
Join Date: Jul 2007
Location: india
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
nameetapani is on a distinguished road
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 "&".
Reply With Quote
  #8 (permalink)  
Old 07-28-2007
Junior Member
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
santosh.shedge is on a distinguished road
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
Reply With Quote
  #9 (permalink)  
Old 07-28-2007
Junior Member
 
Join Date: Jul 2007
Location: kolkata
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
gogo12 is on a distinguished road
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;
Reply With Quote
  #10 (permalink)  
Old 07-30-2007
Junior Member
 
Join Date: Jul 2007
Location: Noida
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ashwini165 is on a distinguished road
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;
Reply With Quote
  #11 (permalink)  
Old 07-30-2007
Contributing Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 43
Thanks: 6
Thanked 6 Times in 6 Posts
prakash.kudrekar is on a distinguished road
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
Reply With Quote
The Following User Says Thank You to prakash.kudrekar For This Useful Post:
  #12 (permalink)  
Old 05-22-2008
Junior Member
 
Join Date: May 2008
Location: new delhi
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
gogia_rajat is on a distinguished road
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
Reply With Quote
  #13 (permalink)  
Old 05-22-2008
Junior Member
 
Join Date: May 2008
Location: in india
Posts: 24
Thanks: 10
Thanked 1 Time in 1 Post
aman15490 is on a distinguished road
Thumbs up Re: Is this a correct statement

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

Last edited by aman15490 : 05-22-2008 at 06:23 AM.
Reply With Quote
  #14 (permalink)  
Old 05-22-2008
Junior Member
 
Join Date: May 2008
Location: in india
Posts: 24
Thanks: 10
Thanked 1 Time in 1 Post
aman15490 is on a distinguished road
Thumbs up Re: Is this a correct statement

Not at all !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++


Thread Tools
Display Modes


Similar Threads

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


All times are GMT -4. The time now is 11:20 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved