November 26th, 2013, 07:39 AM
Problem with casting int to char* (C language not C++)
I have a little problem with casting int to char* (string)... is it even possible in C?
I'll try to explain why i need this.
I had a int varriable (int number_of_revisions)
and I need convert this number of revisions to char * becouse I need create a name of file and the number of revision is part of the name.... so there is part of code for better imagination of this problem.
int number_of_revision = 970; // 970 just for example
char * version;
char * new_name;
version = "0.";
//strcat(version, ch_num_o_rev ); // doesn't work becouse ch_number_of_rev is char and strcat requires char*
please I need quick help... Have anybody any idea how to do it? ...
November 26th, 2013, 08:00 AM
I fear you are misguided. ch_number_of_rev really is char which is the same as char*.
However, in this line you wrote the ch_number_of_rev with a typo.
- programming problems for novice coders
November 26th, 2013, 08:15 AM
Originally Posted by rodiongork
It maybe is the same but function strcat() can't take it cos it need strcat(char*,char*)
So I cant put array of chars into this function.. for example "790" and not '7','9','0' ...I hope you understand me... my english isn't perfect...
November 26th, 2013, 09:04 AM
Really messed up. Super wrong.
version = "0.";
"0." is a constant.
overwrites memory you shouldn't be accessing this way.
Except that of course you should use strncpy and strncat to avoid buffer overruns. I don't feel like looking them up right now to get the order of arguments correct. You should.
sprintf(array, "0.%d", the_integer);
[/code] are essential for python code and Makefiles!