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
What is the best c++ book for beginners?
What is the use of lambda in c++?
What is an accessor in c++?
Why iomanip is used in c++?
Am pass the 10000 records to target in target I will take commit interval 15000 when I was stop the work flow what will happened
What jobs can you get with a c++ certification?
What is the use of ::(scope resolution operator)?
What is runtime polymorphism in c++?
What is the use of endl in c++ give an example?
What is a c++ vector?
What is the difference between a "copy constructor" and an "assignment operator" in C++?
Explain the purpose of the keyword volatile.
Which is the best c++ software?
Describe new operator and delete operator?
What is the use of 'using' declaration in c++?