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 / uma sankar pradhan
The output differs due to the followinf two statements
savea+=sizeof(int)
and
savea++
savea++
=>savea=savea+1
=>savea=savea+1*(sizeof(int))
savea+=sizeof(int)
=>savea=savea+2
=>savea=savea+2*sizeof(int)
sizeof(int) is called the scalefactor of savea
Is This Answer Correct ? | 5 Yes | 1 No |
Post New Answer View All Answers
Should the this pointer can be used in the constructor?
Specify some guidelines that should be followed while overloading operators?
What do the keywords volatile and mean mutable?
Should you pass exceptions by value or by reference?
What flag means?
Can I learn c++ as my first language?
What is c++ w3school?
Discussion on error handling of C++ .
Is there any difference between int [] a and int a [] in c++?
Which format specifier is used for printing a pointer value?
Can you pass an array to a function in c++?
How do you clear a buffer in c++?
Write about the use of the virtual destructor?
What is the outcome of cout< a) 16 b) 17 c) 16.5
When do we run a shell in the unix system? How will you tell which shell you are running?