1)what is RTTI in c++?2)main(){ int i=10; int &p=i; //statement 2printf("%d %d"i,p);}plz simplify the statement the 2 .when i run program gives undefined symbol p..but in a/c mcq that is correct ..say collect address of i ..how??

Showing Answers 1 - 5 of 5 Answers

Kamal

  • Sep 2nd, 2006
 

Answer for 2

the concept of the pointer says that u should always provide the address to a pointer at the right hand side of the expression.

so the error occured the statment should be

p = &i;

  Was this answer useful?  Yes

Alim

  • Sep 4th, 2006
 

main()
{

   int i=10;

  int *p;

p=&i;

printf("%d,%d",i,*p);
}

  Was this answer useful?  Yes

jay

  • Sep 23rd, 2006
 

Ad1)RTTI is acronym for RunTime Type Information and is a way of getting information about classes/objects/methods in program's run time.RTTI is not present in ANSI C however so please repost/seek this question on C++ interview questions.Ad2) int &p = i;is a correct statement and there is no way of simplifying a variable reference definition :]This way however you don't get an address to 'i' but a REFERENCE (something like a constant pointer) to i. If you want to have an address of i you can use this : int p = (int)&i;now p is an integer that holds the address of 'i' variable;

  Was this answer useful?  Yes

Hello anuj,


This program should compile and run in Turbo C++3.0


Anyway, here you have declared and defined a integer variable 'i' initially
its value is 10.


Then you are declaring a reference variable 'p' that refers i. That means
from now onwards p and i refers to same memory location and the value of that
memory location is 10. Reference variable is different from pointer. When
declaring reference variable it should be initialized, otherwise you will get a
compiler error.

If I change the p like 'p=p+1' , the value of p will be 11 and at the same time
value of i will be changed. value of i will be 11 also.


On the other hand if I change 'i' as i=i+1, both i and p will be changed.

Reference variable is very handy to write readable code, you can achieve the
same thing using pointer, but programmers use reference for its simplicity.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions