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 oops and list its features in c++?
What is else if syntax?
Which bit wise operator is suitable for checking whether a particular bit is on or off?
What is difference between array and vector in c++?
How is c++ different from java?
Write about an iterator class?
What is runtime errors c++?
Write about the access privileges in c++ and also mention about its default access level?
Explain the static member function.
What's the most powerful programming language?
Is there a c++ certification?
Can static member variables be private?
What is a hash function c++?
What are dynamic type checking?
What are the differences between java and c++?