C String spliting into array, bad behaviour when string is not supposed to be split

38 Views Asked by At

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);
1

There are 1 best solutions below

0
Allan Wind On

You need to allocate an array to store the pointer:

cmds = malloc(sizeof(*cmds));
cmds[0] = strdup(input);