This question arose: I needed to create an array of strings, and I decided to implement it using an array of pointers.

char **strArr; 

the problem is that the number of the lines themselves = the number of elements of this array I find out only after the work of a certain function. For example, suppose the quantity is contained in the variable count . Then:

 func(&count); // функция, которая возвратит мне значение count strArr = new char *[count]; 

and here when compiling the program, the compiler begins to curse, they say, an undeclared identifier. Tell me what am I doing wrong? Or, if my approach is not very rational, tell me an example of another structure, with the help of which I could implement storing strings.

  • Basically, that it was not C ++? There are vectors there. - HasmikGaryaka
  • unfortunately, yes, in principle. - Setplus
  • one
  • one
    However, you assign pointer-pointer'y pointer (pointer to pointer, pointer), so he swears. - LLENN
  • Fair. Do not judge strictly: I am new to si :) Thank you very much for the link and for the clarification! - Setplus

1 answer 1

Something like this (wrote here and did not check):

 char **allocate_strs(size_t n, size_t len) { char **res; size_t q; if (!(res = malloc(n * sizeof (char*)))) return 0; for (q=0; q<n; ++q) if (!(res[q] = malloc(len))) { while (q--) free(res[q]); free(res); return 0; } return res; }