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


Please Help Members By Posting Answers For Below Questions

What is the purpose of templates in c++?

568


Write about the role of c++ in the tradeoff of safety vs. Usability?

606


What is a list c++?

579


How many ways can a variable be initialized into in C++?

605


Why cout is used in c++?

568






What are inline functions? What is the syntax for defining an inline function?

583


Differentiate between the message and method in c++?

612


Can a function take variable length arguments, if yes, how?

570


What is flag in computer?

608


Write about a nested class and mention its use?

630


What is an adaptor class in c++?

608


Can we specify variable field width in a scanf() format string? If possible how?

666


Out of fgets() and gets() which function is safe to use?

643


What is Destructor in C++?

640


Should the this pointer can be used in the constructor?

558