CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C++ DETAILS
Question :
Where we use reference and where we use pointers ?Write their differences and advantages and disadvantages .

Posted by: kranthi on 9/11/2007 1:46:09 AM

Contact kranthi  Contact kranthi
Category C++ Interview Questions
Rating (4.8) By 6 users
Added on 9/11/2007 1:46:09 AM
Views 4742
Rate it!
Answers:

values for a 'bool' are 'true' and 'false', whereas for 'BOOL' you can use any 'int' value

bool - 1 byte
BOOL - 4 byte ( 32 bit platform where 'int' = 4 bytes )
BOOL - 8 byte ( 64 bit platform where 'int' = 8 bytes )



 Posted by: Andy    

Contact Andy  Contact Andy

The difference is
1. bool is a C++ native data type and has the size of i byte. Actually only 1 bit would suffice as the only valid values are TRUE or FALSE but nothing can have the size less that 1 Byte (leave bit fields apart)
2 whereas BOOL is a windows specific type of int, so its nothing but an int is disguise having size whatever int has on the system. Under C++ guidelines size of int is platform dependent so we leave it at that.



 Posted by: Samit Sasan    

Contact Samit Sasan  Contact Samit Sasan

The reference is used to give the address for the data.The pointers are used to pointing to the address of the memory.



 Posted by: sunitha    

Contact sunitha  Contact sunitha

Reference: is referring to another object, so if you modify the value of the referred object, also the reference's value will be changed:
int a = 10;
int&b = a;
a = 5; // b = 5 too
You cannot redirect b to another int variable.
Reference is widely used as function parameters: while the default parmeter passing is by value, if you want to change
the value of the passed parameter inside a procedure, it won't be modified when the function returns:

void increment(int i)
{
i++;
return;
}

void main()
{
int index = 0;
increment(index) ; // index remains still '0'
}

But if you use references, not the value, but the memory address will be transmitted to the function, which can so modify the value, and index will be 1:
void increment(int& i)

Reference is also used for passing big objects as function parameters, for efficiency purposes (it won't be copied, so the storage and the runtime will be less).
In this case const T& is used, where T is the object type.

Pointers: it points to an object in the memory. Main difference is that it can be redirected to another object.
It has one more indirection level compared to reference: * is the dereference (or indirection) operator, and you can access through this operator the value of the pointed object. The pointer itself contains the memory address:
int a = 10;
int c = 6;
int* p = &a;
cout << *p; // same as cout << a;
a = 5; // *p = 5 but p still referrs to 'a'
p = c; // *p = 6 and p referrs to 'c'



 Posted by: Mona    

Contact Mona  Contact Mona

1)A reference is constant, you can't bind to a different object once it has been initialized.
2)A reference must be initialized, whereas a pointer needn't be.
3)A reference must always point to a valid object, a pointer may be NULL
4)A reference is automatically dereferenced. You don't use *ref to access the actual object it is bound to
5) you can't create a reference to a reference. You can create a pointer to a pointer, though
6)Unlike a pointer, a reference is treated as if it were the object bound to it, i.e. you access members using the dot notation.



 Posted by: vittala    

Contact vittala  Contact vittala


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
what are the disadvantages of copy constructor??
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C++ Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/11797/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

1