+ 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; }

9th Oct 2020, 10:25 PM
nicolas turek
nicolas turek - avatar
28 Answers
+ 2
Ok... I found solution char*func(){ static char buf[] = "string"; return buf; }
9th Oct 2020, 11:12 PM
nicolas turek
nicolas turek - avatar
+ 1
solution for C++ was including string class any idea how to do it in C?
9th Oct 2020, 10:41 PM
nicolas turek
nicolas turek - avatar
+ 1
i don’t know if this is what you want https://code.sololearn.com/cpWkm2mOOnRU/?ref=app
9th Oct 2020, 10:43 PM
Flash
+ 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
9th Oct 2020, 10:49 PM
nicolas turek
nicolas turek - avatar
+ 1
By the way - it's sprintf(buf, "\x1b[3%dm", c);
9th Oct 2020, 10:51 PM
nicolas turek
nicolas turek - avatar
+ 1
you said you wanted to return string. what is string, then?
9th Oct 2020, 10:51 PM
Flash
+ 1
little edit - this is how it should work... https://code.sololearn.com/cDGS3HAr7DjA/?ref=app
9th Oct 2020, 10:55 PM
nicolas turek
nicolas turek - avatar
+ 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.
9th Oct 2020, 10:58 PM
Flash
+ 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
9th Oct 2020, 10:59 PM
nicolas turek
nicolas turek - avatar
+ 1
in current OSs, it’s fine, even in such small apps. but i meant, it’s good to use malloc-free together
9th Oct 2020, 11:02 PM
Flash
+ 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"; }
9th Oct 2020, 11:05 PM
nicolas turek
nicolas turek - avatar
+ 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
10th Oct 2020, 5:43 AM
nicolas turek
nicolas turek - avatar
+ 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
10th Oct 2020, 5:49 AM
nicolas turek
nicolas turek - avatar
+ 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)
10th Oct 2020, 6:07 AM
nicolas turek
nicolas turek - avatar
+ 1
And for c++ only?
10th Oct 2020, 6:09 AM
nicolas turek
nicolas turek - avatar
+ 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)
10th Oct 2020, 6:15 AM
nicolas turek
nicolas turek - avatar
+ 1
Ok, really thanks, I hope I'll find solution
10th Oct 2020, 6:22 AM
nicolas turek
nicolas turek - avatar
- 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...
10th Oct 2020, 6:02 AM
nicolas turek
nicolas turek - avatar
- 4
I'msakkaka0ja(hsjhshshshhshs hmmme Minh were y wwwjjj18;\_1---0_40 cfdrrr5'mj .cajajajjj sas .uynnxnxjxnjnbnbcbc bbbdfbhdhfbguhdjjfnfkfgkgkfkn Svndhsbhsbcbhdf bk0kfkfjshxvjnzjxmd
10th Oct 2020, 4:59 PM
Yasar Curci
Yasar Curci - avatar