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
Is std :: string immutable?
Difference between pass by value and pass by reference?
Which operator cannot be overloaded c++?
Difference between declaration and definition of a variable.
Which programming language should I learn first?
What is the difference between while and do while loop?
Explain how to initialize a const member data.
What is a container class?
What is the difference between map and hashmap in c++?
What are smart pointers?
Write syntax to define friend functions in C++.
Write a Program to find the largest of 4 no using macros.
What is vectorial capacity?
How the endl and setw manipulator works?
Explain rethrowing exceptions with an example?