0

Return string in C

Could not find satisfying ans. My question is simple: how to return string from a function in C. I know that string in C is basically nothing but an array of characters - that means that i cannot directly return “string”. But what is the correct or safest/most common way of doing it? In my task I cannot use Heap, so malloc is not an option. I need something like this: char *get_string(char **argv) { } int main(int argc, char **argv) { char *str = get_string(argv[1]); }

29th Oct 2020, 6:56 PM
Lone Wolf
Lone Wolf - avatar
24 Antworten
+ 2
I suggest you to read Coder Kitten's answers to this question about not just how to return the string, but what's the best way to do it: https://www.sololearn.com/Discuss/2555983/?ref=app
29th Oct 2020, 10:30 PM
Davide
Davide - avatar
+ 2
Coder Kitten 🐈 infinite thanks for the crash course on command line stuff! So if you are not allowed to use getopt and you already know the options in advance you can create an array of string with all the combinations of options, like: combo={"nc" "-l" "-lv" "-lvn" ecc.. } And put a loop before "return argv[i+1];" to check if argv[i+1] matches one of the combos Or even better you can create an array of strings with only the simple options "-l", "-v", "-c"... and then make a function that generates all the possible combinations for you.
30th Oct 2020, 5:05 PM
Davide
Davide - avatar
+ 1
Also thank all of you guys :), it is great that people are still willing to help nowadays..
30th Oct 2020, 1:11 PM
Lone Wolf
Lone Wolf - avatar
+ 1
Coder Kitten I haven't fully understood this line if (!strcmp(argv[i]), "-d") && (i+1 < argc)) I guess because you are speaking in terms of command line or something else that I don't know. But can't you just add a condition that check wether whatever follows -d is -c? edit: if argc represent the length of argv, I get the meaning of the line. But is it necessary to pass the length? you can just create the local variable len=strlen(argv[0]) Anyway I understand that I am speaking about things that I don't know so if I am being wrong don't bother to explain me anything at all
30th Oct 2020, 2:54 PM
Davide
Davide - avatar
0
argv[1] already returns string, you can print it with printf("%s", argv[1]); but only if you put arguments to your program
29th Oct 2020, 7:47 PM
Szwendacz
Szwendacz - avatar
0
Yes I know, but it is little bit more complicated - that string which I want to store in variable “str” is not the argv[1]. It can be any argument passed when you run the program. And I do not want to have unnecessarily long for loop with error chcecking in main, I d like to do it in seperate function - hence get_delim
29th Oct 2020, 8:49 PM
Lone Wolf
Lone Wolf - avatar
0
Meanwhile Ive found a way, could anyone please check if its correct? or without unexpected behavior?
29th Oct 2020, 8:51 PM
Lone Wolf
Lone Wolf - avatar
0
I will add the code in a moment
29th Oct 2020, 8:53 PM
Lone Wolf
Lone Wolf - avatar
29th Oct 2020, 9:06 PM
Lone Wolf
Lone Wolf - avatar
0
Btw, the code wont work in Sololearn, but works fine
29th Oct 2020, 9:07 PM
Lone Wolf
Lone Wolf - avatar
0
Coder Kitten Ok, so correct me if I am wrong: returning string literals is Ok, because they are different from local vars(are alive throughout the program lifetime), so until I try modifying them, it is okay to work with them-returning from a func...
30th Oct 2020, 1:05 PM
Lone Wolf
Lone Wolf - avatar
0
Coder Kitten and could you please clearify why not providing “-d” argument would not be handled properly? I thought that the func would just return str literal “ “, which is exactly what I want for that case.. Could something go wrong? Also ‘getopt’ would be great, but I am not allowed to use it :(
30th Oct 2020, 1:09 PM
Lone Wolf
Lone Wolf - avatar
0
Thanks a lot again. I get it that it is diffiucult for you to completely understand what I want to do - I hadnt provided you with enough context but in spite of this you did great. Davide that line of code which you did not understand means that if current argument is equal to “-d” and there is one more argument available next to it then do the task... If I had not included (i+1 < argc) where argc is total number of arguments passed I would get “segmentation fault” because I do not have access to that memory place... if you have an idea how to make it more readable or efficient, it is very much welcome. Coder Kitten I understand now how you meant it -> but in a scenario when user types “-d -c” it would be considered that -c is the delimeter, it is perfectly fine, even though he might have wanted to use -c as the next argument-it is considered as users fault and he needs to type it again, otherwise -c is the delimeter..
30th Oct 2020, 6:40 PM
Lone Wolf
Lone Wolf - avatar
0
If you want I will give you more context so that you can understand it more(but I will include only the delimeter part for simplicity):
30th Oct 2020, 6:40 PM
Lone Wolf
Lone Wolf - avatar
0
Task is to create a simple program which could emulate Excel for example-working with tables user creates simple text table, separated by chosen delimeters, e.g. by colon, so the table would be saved as .txt file and looked e.g. like this: cell1:cell2:cell3 cell4:cell5:cell6 table 2x3
30th Oct 2020, 6:45 PM
Lone Wolf
Lone Wolf - avatar
0
and a user could then edit the table with arguments, for example changing the delimeter - like this: ./code -d “*:” where in arg behind -d would contain delimeter chosen by user (colon “:”) and then set a new delimeter in edited table to the first character of that argument so -d “*:” would result in a new argument * instead of : new table would look like this after edit: cell1*cell2*cell3 cell4*cell5*cell6
30th Oct 2020, 6:49 PM
Lone Wolf
Lone Wolf - avatar
0
And I know that I could avoid problems with function returning string, but I wanted my code to be more readable... Goal was to store that string behind “-d” in a variable and if there is nothing behind “-d” or not even “-d” argument itself, default delim would be space, thus “ “
30th Oct 2020, 6:57 PM
Lone Wolf
Lone Wolf - avatar
0
Coder Kitten I know its beyond this thread :( i just wanted to give you more context, if there was some other way of connecting with you, or to help me with this, Id be extremely happy, but I understand if you cant. But thank you a lot for the help - you are very clever and skilled, and I learned a lot just from your comments. :)
30th Oct 2020, 7:07 PM
Lone Wolf
Lone Wolf - avatar