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 purpose of templates in c++?
Write about the role of c++ in the tradeoff of safety vs. Usability?
What is a list c++?
How many ways can a variable be initialized into in C++?
Why cout is used in c++?
What are inline functions? What is the syntax for defining an inline function?
Differentiate between the message and method in c++?
Can a function take variable length arguments, if yes, how?
What is flag in computer?
Write about a nested class and mention its use?
What is an adaptor class in c++?
Can we specify variable field width in a scanf() format string? If possible how?
Out of fgets() and gets() which function is safe to use?
What is Destructor in C++?
Should the this pointer can be used in the constructor?