Storing an integer into a char* in C++

心不动则不痛 提交于 2019-12-12 10:47:31

问题


I'm writing some code that returns an integer, which then needs to be outputted using printw from the ncurses library. However, since printw only takes char*, I can't figure out how to output it.

Essentially, is there a way to store a integer into a char array, or output an integer using printw?


回答1:


printw() accepts const char * as a format specifier. What you want is

printw("%d",yournumber);



回答2:


The itoa function converts an int to char*.




回答3:


Use itoa() or sprintf() to convert integer to ascii string.

Example:

char s[50];
sprintf(s, "%d", someInteger);

now u can pass s as char*




回答4:


itoa will help you.



来源:https://stackoverflow.com/questions/1936913/storing-an-integer-into-a-char-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!