+ 2
Returning string from function in c/c++[solved]
I would like to make function which would return char*, I don't think that using malloc is good solution, here is how it looks now: char* fore(int color){ char* tmp = "\x1b[3_m"; *(tmp + 3) = '0' + color; // color is number up to 9 return tmp; }
28 Réponses
+ 2
Ok... I found solution
char*func(){
static char buf[] = "string";
return buf;
}
+ 1
solution for C++ was including string class
any idea how to do it in C?
+ 1
i don’t know if this is what you want
https://code.sololearn.com/cpWkm2mOOnRU/?ref=app
+ 1
Not really - I don't want any buffer array, it's solution, but it would be weird in usage - it's meant to just color text
+ 1
By the way - it's
sprintf(buf, "\x1b[3%dm", c);
+ 1
you said you wanted to return string. what is string, then?
+ 1
little edit - this is how it should work...
https://code.sololearn.com/cDGS3HAr7DjA/?ref=app
+ 1
well, u worte using malloc is not a good solution. anyway. good luck. btw, learn how to free the memory if you use dynamic memory.
+ 1
Thanks... But using malloc... Well... If I remember right - it keeps allocated memory till end of program, in other words it's same as your solution
+ 1
in current OSs, it’s fine, even in such small apps. but i meant, it’s good to use malloc-free together
+ 1
Yeah... But here it's impossible to use free... Maybe I just should make global char array and problem would be solved without errors and allocating too much memory... Still I want to know how it would be possible do it same simple as this:
const char* str(){
return "string";
}
+ 1
This solution is... Ok... I tried that first... But than it became too chaotic and it's lot writing... So I created external file with enum colors and class style with static function
+ 1
Yeah, I know, but it's little bit complicated for my knowlage - for example that typedef for void pointer to some function, I don't really get what it does
+ 1
I don't want to open another thread, I gues you know answer to question if there is one solution for all platforms to be used like getch from conio - I'd like to control my app in terminal/console with arrows, space etc. but I haven't found any multiplatform solution (for unix there is ncursed I think)
+ 1
And for c++ only?
+ 1
Some way to get input when any key is pressed for c++ only, I know that I could use Qt for this... But I'm doing it without it, because it's supposed to be easily portable code (I'm doing only "console" application)
+ 1
Ok, really thanks, I hope I'll find solution
- 1
I think I finaly get it - there are several functions which means style, and function __col_print which takes message and some param which is name of function which style I want...
- 4
I'msakkaka0ja(hsjhshshshhshs hmmme Minh were y wwwjjj18;\_1---0_40 cfdrrr5'mj .cajajajjj
sas .uynnxnxjxnjnbnbcbc bbbdfbhdhfbguhdjjfnfkfgkgkfkn Svndhsbhsbcbhdf bk0kfkfjshxvjnzjxmd