-1

when input has "|" the string splits normally, when str does not have "|" it seg faults

char **cmds;

if (strchr(input, '|'))
    cmds = split(input,'|');
else
    cmds[0] = strdup(input);
  • 2
    You don't have room to store `strdup(input);`, call `cmds = malloc(sizeof *cmds);` before `cmds[0] = strdup(input);`, don't forget to call `free(cmds);` when no longer needed. – David Ranieri Sep 21 '22 at 16:15
  • @DanielMarcelinoSequeira Don't worry. If you keep writing C, it won't be your last one. ;-) – Andrew Henle Sep 21 '22 at 16:47

1 Answers1

0

You need to allocate an array to store the pointer:

cmds = malloc(sizeof(*cmds));
cmds[0] = strdup(input);
Allan Wind
  • 23,068
  • 5
  • 28
  • 38