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 / d289

because when incremented by size of int(4), point to invalid
position in the int array. Hence it will print out only one
correct out put for the first element and garbage for the
rest in the first program while for the second program it
will print out the contents of the int array correctly.

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the three forms of cin.get() and what are their differences?

884


To what does “event-driven” refer?

837


Specify different types of decision control statements?

610


Which is the best c++ compiler for beginners?

807


What is the use of data hiding?

795


What is the output of the following program? Why?

864


How does com provide language transparency?

806


What can I safely assume about the initial values of variables which are not explicitly initialized?

839


What is the type of 'this' pointer?

806


What are pointers, when declared, intialized to a) NULL b) Newly allocated memory c) Nothing. Its random

900


Define basic type of variable used for a different condition in C++?

888


What is auto used for in c++?

794


What will strcmp("Astring", "Astring"); return a) A positive value b) A negative value c) Zero

899


What is the need of a destructor? Explain with the help of an example.

808


What is an inclusion guard?

871