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


Please Help Members By Posting Answers For Below Questions

Define token in c++.

943


Why was c++ made?

874


Show the declaration for a pointer to function returning long and taking an integer parameter.

840


What is a sequence in c++?

803


What is the difference between map and hashmap in c++?

827


Why can’t you call invariants() as the first line of your constructor?

807


Is c++ a software?

968


Is python better than c++?

798


What is insertion sorting?

902


What is a catch statement?

838


Explain queue. How it can be implemented?

925


What is a tuple c++?

788


Explain differences between new() and delete()?

886


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the maximum number of concurrent threads that the InnoDB plug-in can create

2359


Why should we use null or zero in a program?

831