# ErrorRecord ###### tags: `C basics`, `debug` ### sprintf >致謝 >https://stackoverflow.com/questions/19772667/c-sprintf-array-char-pointers/19772761 sprintf does not allocate memory for the string it writes. You have to provide a valid string for it to write into but are currently passing it an uninitialised pointer. **it will fail** ```c= int x = 100; char* string; sprintf(string,"%d",x); printf("%s\n",string); ``` **it works** ```c= int x = 100; char string[] = ""; sprintf(string,"%d",x); printf("%s\n",string); ``` 這裡我測試會掛掉喔~~ string size過小 ![](https://i.imgur.com/4XIvmU7.png) ```c= int x = 100; char string[20]; sprintf(string,"%d",x); printf("%s\n",string); ``` ```c= x = 100; char* string = (char*)malloc(sizeof(char)*10); sprintf(string,"%d",x); printf("%s\n",string); ```