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 / ips

In 1st Case(savea++)
--------------------
The Integer Pointer Is Incremented just Once.(as it Is
Implimented in c/c++).which means the pointer is shifted
4bytes(size of type 'int') ahead.

In 2nd Case(savea+=sizeof(int))
-------------------------------
Here The Statement implies:-
savea+=4;
The above statement says that,the integer pointer is to
be increamented 4times.means,the Pointer now is shifted 16
bytes(4*sizeof type 'int').which is Out of scope of the
integer array in the Programme.

Is This Answer Correct ?    3 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of vtable?

901


What is ostream in c++?

763


What does it mean to declare a destructor as static?

847


What is pointer to member?

788


How do I start a c++ project?

804


What is the standard template library (stl)?

1077


Explain rtti.

815


Can you please explain the difference between overloading and overriding?

847


Please explain the reference variable in c++?

823


What's the "software peter principle”?

849


What is microsoft c++ redistributable?

803


Can we use this pointer in a class specific, operator-overloading function for new operator?

855


What is data abstraction? How is it different from data encapsulation?

776


Differentiate between C and C++.

915


Is java made in c++?

803