#include <stdio.h>
#include <string.h>
char* changeString(char *inputString);
int main() {
printf("Changed string is %s\n", changeString("42"));
}
char* changeString(char *inputString) {
static const char* someStrings[3] = {"abc", "def", "ghi"};
char* output;
strcat(output, someStrings[1]);
return output;
}
I’m trying to append a char* to another char* however the strcat keeps resulting in a segmentation fault because the char* has no size, changing char* output; to char output[100]; fixes the segmentation fault, but then I am returning the wrong type and I can’t print the answer in printf.
Any advice would be much appreciated.
EDIT: I know the example above seems to do nothing of value, I changed it to demonstrate the logic I am using.
You have not allocated memory for the output string. Do use malloc() to allocate memory and then try strcat.