我有簡單的測試程序在Linux上sprintf的錯誤用法?
#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);
};
試運行,GCC 4.3.4:在Solaris 10
[email protected] /tmp $ ./a.out
cOut address= f9f41880
cpOut address = f9f41880
cOut address= f9f41880
cpOut address = f9f41880
試運行,太陽C++ 5.10:
bash-3.00$ ./a.out
cOut address= 8047488
cpOut address = 8047488
cOut address= 8047488
cpOut address = 8000a20
任何人都可以請解釋爲什麼通過調用sprintf函數覆蓋指針cpOut?
非常感謝回答 – jano 2010-11-18 15:58:06