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 atoi in c++?
What is the difference between a pointer and a link in c ++?
List the special characteristics of constructor.
What is the importance of mutable keyword?
Explain differences between new() and delete()?
Why is null pointer used?
Explain about Garbage Collector?
Why should we use null or zero in a program?
How do I run c++?
Why is swift so fast?
What is the last index number in an array of 100 characters a) 100 b) 99 c) 101
How can I disable the "echo" feature?
Is c++ an integer?
What is size_type?
Is c++ a good beginners programming language?