The algorithm should return a two-dimensional array with a space-separated string (the same split ("") from Python).

#include <stdio.h> #include <stdlib.h> #include <string.h> // Добавление char в конец массива char void append(char* needed, char to_append) { int length = strlen(needed); needed[length] = to_append; needed[length+1] = '\0'; } char** split(char* input) { int space_count = 0; // Считаем пробелы for (int i = 0; i < strlen(input); i++) { if (input[i] == ' ') { space_count++; } } // Указатель на указатель на char. char **splitted_input; // Выделяем память под массивы char splitted_input = (char**) malloc((space_count+1) * sizeof(*splitted_input)); // Хранилище для текущей строки (перед пробелом) char word[100] = ""; int current = 0; for (int i = 0; i < strlen(input); i++) { if (input[i] == ' ') { printf("NEW: [%d] = %s\n", current, word); splitted_input[current] = word; current++; // Обнуляем весь массив memset(word, 0, sizeof(word)); continue; } // Если не пробел, то добавляем в конец word новый символ append(word, input[i]); } printf("NEW: [%d] = %s\n\n", current, word); // Добавляем текущую строку splitted_input[current] = word; for (int i = 0; i < space_count+1; i++) { printf("%s\n", splitted_input[i]); } // Еще не имею понятия, как здесь лучше освободить память splitted_input return splitted_input; } int main() { char input[20] = "hi everyone"; char** need = split(input); return 0; } 

Displays:

 NEW: [0] = hi NEW: [1] = everyone everyone everyone 

I do not understand why splitted_input [0] is also equal to everyone

  • The algorithm is conceptually incorrect, since there can be several spaces between words (not to mention tabs), and not just one space. - Vlad from Moscow

2 answers 2

Direct answer to the question: they both point to the same word .

Another problem: an array with pointers to a local variable is returned from the function.

Decision. For splitted_input[current] allocate memory on a new line, where copy the contents of the word :

 splitted_input[current] = strdup(word); 
  • Yes, now I understand, thanks char w1 [100]; strcpy (w1, word); splitted_input [current] = w1; The right way? What other way else can this be implemented (for the sake of interest)? - Gosh Obynichny
  • @Gosh.Common No, this is wrong. w1 is also a local variable. The memory allocated to this array will be considered free as soon as the execution goes out of its scope. - Igor
  • it was an alleged solution to the problem of allocating memory to a new line, and not with a local variable. And about the second problem: I have a splitted_input - a dynamic variable (because I allocate memory using malloc), so the memory will be available even after the function ends, no? - Gosh Obynichny
  • one
    @Gosh.Common In your code, you dynamically allocate memory for a one-dimensional array from char* . The arrays of char themselves should also allocate memory dynamically. - Igor
  • understood thanks! - Gosh Obynichny
 splitted_input[current] = word; 

At each iteration of the loop, the splitted_input[current] pointer is placed on the same word array. Of course, the same line will be displayed if all pointers point to the same place.