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

Should the this pointer can be used in the constructor?

642


Specify some guidelines that should be followed while overloading operators?

717


What do the keywords volatile and mean mutable?

696


Should you pass exceptions by value or by reference?

782


What flag means?

601






Can I learn c++ as my first language?

740


What is c++ w3school?

736


Discussion on error handling of C++ .

734


Is there any difference between int [] a and int a [] in c++?

639


Which format specifier is used for printing a pointer value?

664


Can you pass an array to a function in c++?

626


How do you clear a buffer in c++?

631


Write about the use of the virtual destructor?

696


What is the outcome of cout< a) 16 b) 17 c) 16.5

698


When do we run a shell in the unix system? How will you tell which shell you are running?

634