-1

I'm new to C and am having trouble with pointers. I've looked at guides and (at least tried) followed their advice, but still can't get my code to work.

I've got driver.c :

int main(int argc, char *argv[]) {
  char *prefix = "p", *suffix = "aa";
  char *result;

  result = formResult(prefix, suffix);
}

And in helper.c :

char* formResult(char *prefix, char *suffix) {
  char *result = strcpy(result, prefix);
  result = strcat(result, suffix);
  return result;
}

I keep getting "Segmentation fault (core dumped)" when I get to this call and can't figure out how to solve it. I've tried malloc with prefix and suffix, but that wouldn't work either (though I may have been doing it wrong).

Thanks.

EDIT: I've updated driver.c to be

int main(int argc, char *argv[]) {
  char *prefix = malloc(15), *suffix = malloc(15);
  prefix = strcpy(prefix, "p");
  suffix = strcpy(suffix, "aa");
  char *result = malloc(30);

  printf("step0");
  result = formResult(prefix, suffix);
} 

And helper.c to be

char* formResult(char *prefix, char *suffix) {
  printf("step1");
  char *result =  malloc(strlen(prefix) + strlen(suffix) + 1);
  result = strcpy(result, prefix);
  result = strcat(result, suffix);
  return result;
}

I can get "step0" to print, but I still get a core dump without "step1" printing.

fiveandten
  • 61
  • 1
  • 10
  • 2
    Turn on your compiler warnings. Don't waste human attention on something that machines can do for you. – Kerrek SB Sep 23 '14 at 22:45
  • You need to allocate space for the stuff you're putting into `result`. – juanchopanza Sep 23 '14 at 22:46
  • @Kerrek Never heard of those before, but looking at a list of GCC's options is quite overwhelming. Looks like -wall would be useful, but I'm not sure if there may be a more specific useful one. – fiveandten Sep 23 '14 at 22:49
  • @juanchopanza using malloc if I don't know how many characters the char* will be, is it okay to set the size to something larger than it would be just in case, or is it convention to free() and realloc() each time I want to change it? – fiveandten Sep 23 '14 at 22:51
  • 1
    I think you meant `char *prefix = "p", *suffix = "aa";` . Your actual code should give a compilation error. Don't ignore errors. – M.M Sep 23 '14 at 22:52
  • 1
    Use your mouse to copy and paste the entire source file to the question input box. – n. m. could be an AI Sep 23 '14 at 23:01
  • possible duplicate of [segmentation fault with strcpy](http://stackoverflow.com/questions/10326586/segmentation-fault-with-strcpy) – n. m. could be an AI Sep 23 '14 at 23:06
  • @n.m. It's not a segmentation fault with strcpy. It's not even getting down to that call. It's faulting at the call of result = formResult(prefix, suffix); – fiveandten Sep 23 '14 at 23:25
  • 1
    You are wasting everyone's time. You have already modified your source at least twice, completely changing it and invalidating all prior comments and answers. **Post an entire compilable program as is**. Make sure it compiles without warnings. Use `-Wall`. – n. m. could be an AI Sep 23 '14 at 23:32
  • @n.m. I added an edit section, not modified the original. Thanks for being a helpful, friendly human being :) – fiveandten Sep 23 '14 at 23:53
  • 2
    Let me try to be helpful (not necessarily friendly) one last time. [This](http://coliru.stacked-crooked.com/a/26000bf4eb097c57) is how the code in your submission should look like. Note this code does not have any warnings with `-Wall`, does not segfault, and shows the intended result. Please make sure your code does not have any warnings with `-Wall`, *then* post it in its entirety. If it does have warnings and you don't know how to get rid of them, mention that. – n. m. could be an AI Sep 24 '14 at 00:08

2 Answers2

1

You are writting in an unalloced memory space, you should allocate it with malloc:

char *result = malloc(sizeof(*result) * (strlen(prefix) + strlen(suffix) + 1))
Titouan42
  • 129
  • 4
0
char *result = strcpy(result, prefix);

That line will exhibit undefined behavior. You haven't allocated any space for result yet. So let's do that:

char* formResult(char *prefix, char *suffix) {
  char *result = malloc(strlen(prefix) + strlen(suffix) + 1);
  strcpy(result, prefix);
  strcat(result, suffix);
  return result;
}

If you're using gcc or clang, I'd recommend adding the compiler flag -Wall to your command line.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Thanks. I tried your suggestion but I'm still getting a core dump. I updated the Q with my edits. I also started using -Wall and i'm not getting any warnings/errors related to this :/ – fiveandten Sep 23 '14 at 23:18