I have a simple test program
#include <stdio.h>
int main( int argc , char* argv[] )
{
unsigned int number=2048;
char* cpOut;
char cOut[4];
cpOut=(char*)&cOut[0];
printf("cOut address= %x \n",&cOut[0]);
printf("cpOut address = %x \n",cpOut);
sprintf(&cOut[0],"%d \n", number);
printf("cOut address= %x \n",&cOut[0]);
printf("cpOut address = %x \n",cpOut);
};
Testing on Linux, gcc 4.3.4:
user@server /tmp $ ./a.out
cOut address= f9f41880
cpOut address = f9f41880
cOut address= f9f41880
cpOut address = f9f41880
Testing on Solaris 10, Sun C ++ 5.10:
bash-3.00$ ./a.out
cOut address= 8047488
cpOut address = 8047488
cOut address= 8047488
cpOut address = 8000a20
Can someone explain to me why the cpOut pointer is overwritten by a call to the sprintf function?
source
share