3

I have an array of strings:

array1[] = {
    "echo", "hi", "|",
    "sed", "s/i/ey/g", "|",
    "sed", "s/y/ll/g", "|",
    "sed", "s/$/o/g", "|",
    "cat"
};

I want to split this array into arrays by "|" string like below:

array2[][] = {
    { "echo", "hi" },
    { "sed", "s/i/ey/g" },
    { "sed", "s/y/ll/g" },
    { "sed",‌ ​"s/$/o/g" },
    { "cat" }
};

How can I do this in C?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dake
  • 33
  • 3
  • The question is: where am I mistaken?:D – Dake Dec 22 '16 at 16:17
  • Can you explain more what you are trying to achieve? Maybe you can show us your desired output? – RoadRunner Dec 22 '16 at 16:19
  • what about using http://en.cppreference.com/w/c/string/byte/strtok or see here: http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c – Jarlik Stepsto Dec 22 '16 at 16:21
  • @Dake, yeah it's just hard to see what you are trying to achieve with this code. It would be good if you can include some examples. – RoadRunner Dec 22 '16 at 16:22
  • The string array in the first paragraph does not contain strings. It would need to be `{"echo", "hi", ...` etc. – Weather Vane Dec 22 '16 at 16:25
  • I appreciate your help, so edited my post, now i think it is more clear.. – Dake Dec 22 '16 at 16:53
  • In fact, you do not have array of strings. What you have in your example is an array of `const char *` – badera Dec 22 '16 at 18:02
  • What have you tried? It can be done easily enough — though the second array definition is not valid in C because you must supply all dimensions of an array except perhaps the first (and you need to define the base type, `char`, too). If you're going to be using this to run the commands via `execvp()` or similar, you want a null pointer at the end of each set of strings in `array2`. From a `sed` perspective, you only need 1 sed command; from a shell perspective, you don't need the `cat`. I assume they're like that to give a number of command strings rather than because it's good shell script. – Jonathan Leffler Dec 22 '16 at 18:38
  • You could try an array of structs. A lower level struct containing each row along with number of columns, and a higher level struct which contains the 2d array of strings, along with the number of rows. Although this would be somewhat complicated, it would handle the inconsistent columns issue perfectly. – RoadRunner Dec 23 '16 at 05:13

1 Answers1

1

You want something like:

char *array2[][2] = {
    {"echo", "hi"},
    {"sed", "s/i/ey/g"},
    {"sed", "s/y/ll/g"},
    {"sed", "s/$/o/g"},
    {"cat", NULL}
};

From memory:

  • The inner dimension of the array needs to be specified (technically every dimension except the outer)
  • You don't want to use smart quotes (copying your program gave all sorts of unicode stuff)
  • The inner demension must be constant (see catline which needed a NULL)

  • You need to declare what it is a 2-dimension array of (in this case char * which was missing form the definitnion)

abligh
  • 24,573
  • 4
  • 47
  • 84
  • usually "innermost" refers to the dimension next to the identifier, and "outermost" refers to the dimension at the right – M.M Dec 22 '16 at 22:44