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

Can a program run without main function?

623


Why is main an int?

531


Is swift faster than go?

619


How the memory management in vectors are being done. What happens when the heap memory is full, and how do you handle it ?

1841


How do pointers work?

709






Can you use the function fprintf() to display the output on the screen?

670


What ANSI C++ function clears the screen a) clrscr() b) clear() c) Its not defined by the ANSI C++ standard

593


How many types of scopes are there in c++?

580


How much do c++ programmers make?

567


Is c++ free?

586


Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification?

644


Is map sorted c++?

521


What are the benefits of operator overloading?

682


Why we use #include iostream in c++?

585


What is protected inheritance?

602