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
Write a short code using c++ to print out all odd number from 1 to 100 using a for loop
What is encapsulation in C++? Give an example.
What is function overriding in c++?
What is a pointer with example?
What is endianness?
Is c++ vector a linked list?
What does flush do c++?
Write about the role of c++ in the tradeoff of safety vs. Usability?
What are c++ templates used for?
What is the object serialization?
What are disadvantages of pointers?
What is class in c++ with example?
Reads in the size of a square from the screen; 2. Prints a hollow square of that size out of “-“, “|” and blanks on screen; 3. Prints the same hollow square onto a text file. Your program should work for squares of all side sizes between 1 and 20. --- │ │ │ │ │ │ ---
What is a dll entry point?
What is type of 'this' pointer?