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 copy constructor? Can we make copy constructor private in c++?
What is c++ iterator?
What is the best c c++ compiler for windows?
What is set in c++?
How to defines the function in c++?
Explain the isa and hasa class relationships. How would you implement each?
What is a memory leak c++?
What is general form of pure virtual function? Explain?
What is a base class?
Does improper inheritance have a potential to wreck a project?
Explain the benefits of proper inheritance.
What is the full form of dos?
What is c strings syntax?
What is the use of lambda in c++?
What are exceptions c++?