1)#include <iostream.h>
int main()
{
int *a, *savea, i;
savea = a = (int *) malloc(4 * sizeof(int));
for (i=0; i<4; i++) *a++ = 10 * i;
for (i=0; i<4; i++) {
printf("%d\n", *savea);
savea += sizeof(int);
}
return 0;
}
2)#include <iostream.h>
int main()
{
int *a, *savea, i;
savea = a = (int *) malloc(4 * sizeof(int));
for (i=0; i<4; i++) *a++ = 10 * i;
for (i=0; i<4; i++) {
printf("%d\n", *savea);
savea ++;
}
return 0;
}
The output of this two programs will be different why?
Answer Posted / uma sankar pradhan
The output differs due to the followinf two statements
savea+=sizeof(int)
and
savea++
savea++
=>savea=savea+1
=>savea=savea+1*(sizeof(int))
savea+=sizeof(int)
=>savea=savea+2
=>savea=savea+2*sizeof(int)
sizeof(int) is called the scalefactor of savea
| Is This Answer Correct ? | 5 Yes | 1 No |
Post New Answer View All Answers
What is the benefit of encapsulation?
What is abstract class in c++?
What do you mean by a template?
Difference between overloaded functions and overridden functions
What are libraries in c++?
What do you mean by late binding?
What is the rule of three?
what are the characteristics of Class Members in C++?
What is the difference between equal to (==) and assignment operator (=)?
Can non-public members of another instance of the class be retrieved by the method of the same class?
What is the purpose of extern storage specifier?
Why c++ is so important?
List the issue that the auto_ptr object handles?
How many static variables are created if you put one static member into a template class definition?
Can you write a function similar to printf()?