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
What are the three forms of cin.get() and what are their differences?
To what does “event-driven” refer?
Specify different types of decision control statements?
Which is the best c++ compiler for beginners?
What is the use of data hiding?
What is the output of the following program? Why?
How does com provide language transparency?
What can I safely assume about the initial values of variables which are not explicitly initialized?
What is the type of 'this' pointer?
What are pointers, when declared, intialized to a) NULL b) Newly allocated memory c) Nothing. Its random
Define basic type of variable used for a different condition in C++?
What is auto used for in c++?
What will strcmp("Astring", "Astring"); return a) A positive value b) A negative value c) Zero
What is the need of a destructor? Explain with the help of an example.
What is an inclusion guard?