Mayank's C/C++ Challenger - II
Hi All,
I am back with another question. Here it goes:
What will happen to the following code?
Tell me whats right or whats wrong with this piece of code.
To make it easy, here are the options:
All the best to everyone, hope you all will enjoy it.
Thanks and Regards,
Mayank Shukla.
I am back with another question. Here it goes:
What will happen to the following code?
int main() { int *p = NULL ; int *&ref = p ; *ref = 5 ; cout << *p << endl ; return 0 ; }
Tell me whats right or whats wrong with this piece of code.
To make it easy, here are the options:
- Compilation error
- Runtime error
- Normal Execution
All the best to everyone, hope you all will enjoy it.
Thanks and Regards,
Mayank Shukla.
Replies
-
xheavenlyx
int main() { int *p = NULL ; /* This line inits a pointer p(address) which points to a NULL value*/ int *&ref = p ; /* !!In short, ref=p :) */ *ref = 5 ; /* put value 5 where ref(address i.e p) points to. */ cout << *p << endl ; /* Output 5? */ return 0 ; }
โ
Well If I am correct on the line of
int *&ref = p;
then I guess my ans can be correct. Good Q though but practically its a hell-code. ^_^
Regards
-
MaheshI think its run time error because there is no reference pointer in C++.
-
Mayank
Hi,xheavenlyxint main() { int *p = NULL ; /* This line inits a pointer p(address) which points to a NULL value*/ int *&ref = p ; /* !!In short, ref=p :) */ *ref = 5 ; /* put value 5 where ref(address i.e p) points to. */ cout << *p << endl ; /* Output 5? */ return 0 ; }
โ
Well If I am correct on the line of
int *&ref = p;
then I guess my ans can be correct. Good Q though but practically its a hell-code. ^_^
Regards
Output is not 5.
You have interpreted other statements well, but have just missed out the actual result. Try to read your own conclusions and hit the correct solution.
All the best.:smile:
Thanks and Regards,
Mayank Shukla. -
xheavenlyxOh yeah!, I think ref is a reserved token word and cannot be used as a pointer. As Mahesh has pointed above ๐
I dont have a compilet to check this but lets see. Wait for some more to reply befor u give the ans.
Regards -
Mayank
Hi,MaheshI think its run time error because there is no reference pointer in C++.
You are close I should say.
In C++, there cant be pointer to references, coz pointer points to memory locations and we dont have any specific memory locations allocated to references.
BUT, here we are using refrence to pointer, which is absolutely allowed in C++.:smile:
So you are right for Runtime error, but try for a correct explanation.
Good TRY.
Thanks and Regards,
Mayank Shukla. -
Mayank
Hi,xheavenlyxOh yeah!, I think ref is a reserved token word and cannot be used as a pointer. As Mahesh has pointed above ๐
I dont have a compilet to check this but lets see. Wait for some more to reply befor u give the ans.
Regards
Mahesh has pointed out something else friend. Check my earlier replies.
Anyways "ref" is definitely not a reserved keyword in C++.
Regards,
Mayank Shukla -
xheavenlyxOH!!!
The line &*ref=p will assign p to ref. (i.e: ref=p) but this isnt allowed, so a runtime error! ๐
Since ref is already a refrenced memory location and cannot be changed explicitly. ITs like doing this:
int a=0;
const b=4;
b=a; /not possible
if(b==a)
then
blah;
else
blah; /Is correct. -
aushinaireint *&ref = p;
since a reference variable is basically another name for an existing variable, whats happening here is that we've created another name with which we can call the pointer p.
so this line :
*ref = 5;
is the same as saying
*p=5;
unfortunatey though, the pointer p points to null, so there's no memory location to put the value 5 into.
so my guess would be that there would be a runtime error, coz the variables p and ref, are pointing to empty space.
let me know if my stab in the dark hit anywhere near target.. -
sahanahi.
so *p=NULL;
this is same as
int *p;
p=NULL;
this statement means that p will point to nothing.hence p will store nothing. if we try to print the value of *p it will throw an exception saying that it is "NULL POINTER ASSIGNMENT".so it is a run time error. -
xheavenlyxWell, I guess *p=Null does not mean p=NULL. it means well, *p=NULL, ie p points to a NULL value but I have a feeling NULL has stopped working in c++.
Long back we tried to assign an array NULL value meaning empty, but it didnt work.
int a[20]=null /nope
int a[20]={} /yes
Something of this sort, I cant think a lot on this for now ๐ -
aushinairexhx, might'v worked if u had used
int a[20];
a=null; -
Kaustubh KatdareMayank, I guess its time for you to drop in! ๐
Waiting for Challenger - III
-The Big K- -
MayankBINGO!!! ๐
aushinaire and sahana has answered it correctly.
I apologize for not remaining in touch with you people for a bit long.
Friends it was some important work that I was stucked up with.
Sorry Again. ๐ก
XHX,
"The line &*ref=p will assign p to ref. (i.e: ref=p) but this isnt allowed, so a runtime error! "
Actually, the line goes as
int *&ref = p ;
which is absolutely correct. Here ref is defined as a reference to a integer pointer 'p'. So nothing wrong with it.
aushinaire has correctlty answered to xheavenlyx in the last post relating to the NULL assignment to an array.
I'll be coming up with another question pretty soon. ๐
Thanks and Regards,
Mayank shukla. -
Kaustubh KatdareThat was super, Mayank. Waiting for Challenger - III ๐
-The Big K-
You are reading an archived discussion.
Related Posts
What is difference between logical and physical address?
Introduce Yourself- Template (Look Here First)
Hi, I am a fresh graduate recently joined the company as my first job and very much serious about it.
โ
Name: Rahul Diyewar...
Hello CEans!
I'm aware that we can do a better job with the forum structure. Our current structure is a little complex and doesn't give a feel of "Engineering Forum"....
Hey CEans,
I have an idea that why shouldn't we all, who have some idea of programing, take up some projects from The_Big_K.
What i propose is...
* The bk...
Do you know, we are still living in the Stone Age? We still communicate in Morse code. We still use steam piston for most of the power (at least most)....