Mayank
Mayank
Branch Unspecified
06 Nov 2006

Mayank's C/C++ Challenger - II

Hi All,

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
Just make a point, apart from the correct option I m looking for a valid explanation for the same.

All the best to everyone, hope you all will enjoy it.


Thanks and Regards,
Mayank Shukla.
xheavenlyx

xheavenlyx

Electronics and Communication
12 years ago
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
Mahesh

Mahesh

Branch Unspecified
12 years ago
I think its run time error because there is no reference pointer in C++.
Mayank

Mayank

Branch Unspecified
12 years ago
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
Hi,

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

xheavenlyx

Electronics and Communication
12 years ago
Oh 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

Mayank

Branch Unspecified
12 years ago
Mahesh
I think its run time error because there is no reference pointer in C++.
Hi,

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

Mayank

Branch Unspecified
12 years ago
xheavenlyx
Oh 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
Hi,

Mahesh has pointed out something else friend. Check my earlier replies.
Anyways "ref" is definitely not a reserved keyword in C++.


Regards,
Mayank Shukla
xheavenlyx

xheavenlyx

Electronics and Communication
12 years ago
OH!!!

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

aushinaire

Branch Unspecified
12 years ago
int *&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..
sahana

sahana

Branch Unspecified
12 years ago
hi.
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.
xheavenlyx

xheavenlyx

Electronics and Communication
12 years ago
Well, 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 😀
aushinaire

aushinaire

Branch Unspecified
12 years ago
xhx, might'v worked if u had used

int a[20];
a=null;
12 years ago
Mayank, I guess its time for you to drop in! 😀

Waiting for Challenger - III

-The Big K-
Mayank

Mayank

Branch Unspecified
12 years ago
BINGO!!! 😁
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.
12 years ago
That was super, Mayank. Waiting for Challenger - III 😁

-The Big K-

Share this content on your social channels -

Only logged in users can reply.