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

Is std :: string immutable?

559


Difference between pass by value and pass by reference?

597


Which operator cannot be overloaded c++?

583


Difference between declaration and definition of a variable.

585


Which programming language should I learn first?

574






What is the difference between while and do while loop?

553


Explain how to initialize a const member data.

596


What is a container class?

617


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

553


What are smart pointers?

666


Write syntax to define friend functions in C++.

601


Write a Program to find the largest of 4 no using macros.

578


What is vectorial capacity?

641


How the endl and setw manipulator works?

556


Explain rethrowing exceptions with an example?

604