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
What is flush programming?
What is the function to call to turn an ascii string into a long?
Explain overriding.
What are static type checking?
Which function cannot be overloaded c++?
Write a c program for binary addition of two 8 bit numbers.
Is c++ fully object oriented?
Is c++ a software?
Will c++ be replaced?
What is iomanip c++?
what are the decision making statements in C++? Explain if statement with an example?
Which bit wise operator is suitable for putting on a particular bit in a number?
When is dynamic checking necessary?
Incase of a function declaration, what is extern means?
When does a name clash occur in c++?