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 is scope resolution operator in c++ with example?
If dog is a friend of boy, and terrier derives from dog, is terrier a friend of boy?
What is void pointer in c++ with example?
What does floor mean in c++?
Can we make copy constructor private in c++?
What is a responder chain?
What are the defining traits of an object-oriented language?
Explain some examples of operator overloading?
What is null c++?
Explain how the virtual base class is different from the conventional base classes of the opps.
what is COPY CONSTRUCTOR and what is it used for?
What is a class template?
What is endl c++?
Why use of template is better than a base class?
What is difference between c++ 11 and c++ 14?