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 smart pointers?
What is the maximum value of a unsigned char a) 255 b) 256 c) 128
Inline parameters : What does the compiler do with the parameters of inline function, that can be evaluated in runtime ?
Do the parentheses after the type name make a difference with new?
What is a linked list in c++?
Is java based off c++?
What are the vectors in c++?
Explain how a pointer to function can be declared in C++?
What is the use of bit fields in structure declaration?
What is == in programming?
Difference between a homogeneous and a heterogeneous container
Explain the concept of memory leak?
What do the header files usually contains?
Explain what you mean by a pointer.
What is algorithm in c++ programming?