C/output of a program.
Expert: Zlatko - 11/23/2011
QuestionQUESTION: sir,
please give me output of following code:-
int *a, b=30;
&b=1000;
a=&b;
b=b-5;
*a=*a+b;
*a=b-10;
a=a-10;
*a=*a-5;
a=a+5;
*a=*a+5;
cout << *a << b;
getch();
ANSWER: Hi
The code is incorrect. You cannot say
&b=1000;
It is like saying "the address of b is 1000". The address of a variable is where the variable is stored in memory. You cannot set the address of a variable. That is done by the system.
Try compiling your code and running it yourself. Then ask me about what you don't understand.
Kindest regards
Zlatko
---------- FOLLOW-UP ----------
QUESTION: sir,
but my question is on theory base,&b=1000 means let us suppose address of bis 1000 and then compile the program. so please sir solve the question on the basis of supposition. and reply me as soon as possible.
AnswerHello
You need to trace through the program line by line and write down what is happening. For example:
int *a, b=30; b starts out at 30
&b=1000; This line is not important. It does not matter what the address of b is.
a=&b; The a now points to b. This means that *a and b are equivalent. To say *a=1, is the same as saying b=1.
b=b-5; The b is now reduced from 30 to 25
*a=*a+b; Remember that a points to b, so *a, and b are the same. *a=*a+b is the same as b=b+b, so b becomes 50
*a=b-10; Again, a points to b, so this line is the same as b = b-10. The b becomes 40.
a=a-10; A now points to 10 integers before b. What is at that location? We are not told. Are you sure you copied the line correctly?
*a=*a-5; The mystery location is decremented by 5. We did not know the starting value, so we cannot know the final value.
a=a+5; A now points to 5 integers before b. Again we do not know what is at that location.
*a=*a+5; The mystery location has a mystery value, and the value is increased by 5
cout << *a << b; We cannot know the value of the integer to which a is pointing, so *a is unknown. The b is 40
I suspect you made a mistake when copying the program to the experts web site because the value of *a is not known from the information given. If you did make a mistake, and find the mistake, you can use the step-by-step method I have showed you to solve the question.
Best regards
Zlatko