0

I am trying to have one function pass an string into a string like this:

char taBortTecken(char inputString[]){

//antalBokstaver-1 make sure the string is just the right size. This is working properly
char nyString[antalBokstaver-1];

return nyString;

}

int main(void){
char inputString[] = "halloj i stugan";

char hellA[] = taBortTecken(inputString);


return 0;
}

Basically main is calling Stringfuction and giving it a string, it does its work and passes back a "processed" string (not the same content obviously). Note that this code in not working but sums up what I am trying to do.

J. Doe
  • 335
  • 1
  • 2
  • 11
  • 1
    In order to make more clear what your environment is, you should give at least the declaration and definition of `inputString`. – Yunnosch Feb 01 '18 at 14:30
  • I think you want something which will require the function to know the size of the buffer/array which contains the string, especially if the processing can increase the string length. – Yunnosch Feb 01 '18 at 14:33
  • Note that strings are null-terminated arrays in C, and you can't return arrays from functions. Your options are: 1) pass `SuperString[]` into the function and modify it there; 2) dynamically allocate within the function and return a pointer to the allocation; 3) wrap `SuperString[]` in a `struct` and return the `struct`. – ad absurdum Feb 01 '18 at 14:34
  • 1
    Possible duplciate: https://stackoverflow.com/questions/25798977/returning-string-from-c-function – StoryTeller - Unslander Monica Feb 01 '18 at 14:34
  • I am not allowed to use option 2 and 3... How do you mean by option 1? – J. Doe Feb 01 '18 at 14:36
  • Option 1) is to pass two arrays to the function: `func(SuperString, InputString);`. Then the new string is stored in the `SuperString[]` array. – ad absurdum Feb 01 '18 at 14:39

0 Answers0