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 are friend classes? What are advantages of using friend classes?

880


Are c and c++ different?

755


Can we overload operator in c++?

807


What is buffer and example?

745


What is the basic structure of a c++ program?

852


What is the array and initializing arrays in c++?

769


What are the various situations where a copy constructor is invoked?

867


What is virtual destructor ans explain its use?

876


Who discovered c++?

778


Is c++ the hardest programming language?

880


What is size of string in c++?

772


What does the nocreate and noreplace flag ensure when they are used for opening a file?

919


Which is best c++ or java?

886


What does return 0 do in c++?

837


What are static and dynamic type checking?

847