So, in C / C ++, we create an array of strings as follows.

char ** ptr = new char*[n]; for (int i = 0; i < n; ++i) ptr[i] = new char[m]; 

As a result, we get n lines of m characters in each line. To move along the line we can use the following construction.

 while( ptr[i] != NULL && *ptr[i] != '\0') { // Делаем нечто ++p[i]; } 

Essentially, we are looking at whether a line ending character is encountered. We can pass the term to the form without passing its length, and go through all the characters in the string. A question such how to make the similar mechanism for sorting of lines in an array?

  • one
    In general, you are going to be engaged in some ungrateful work :). Use std :: vector! If for some reason it is necessary on C, then use the structure of the array and its length. - kirelagin
  • This question is theoretical. Just wondering. PS In vector it is undesirable to put another vector, as far as I remember. - Nicolas Chabanovsky
  • one
    Vector absolutely no difference what to put in it :). But my position is such that in the situation under discussion it is best to put std :: string;) in the vector. If you write something like that in C ++, the choice should definitely be in favor of the STL type instead of the sish type. - kirelagin
  • And so the answer, in general, is already given - you need to add the last fake element, as it is done in the rows (only here you have to do it with pens). You can even try for fun version, similar to Pascal lines, when the first element of the array is stored its length :). - kirelagin

3 answers 3

Something like that.

 static char end[] = "end"; char ** ptr = new char*[n+1]; for (int i=0; i<n; ++i) ptr[i] = new char[m]; ptr[n] = end; // как альтернатива '= NULL' 
  • This is what I had in mind - AlexDenisov
  • If suddenly your ptr [j] becomes NULL, then this scheme will break. - Equinox
  • @Equinox will break, so they don’t write, but the question was “how to make a similar mechanism for sorting the strings in an array” - how much more similar? the line also breaks if you put 0 in the middle - IAZ
  • In fact, it is not necessary to dwell on NULL, you can replace the last line with a couple of lines of the type: static char const end [] = "END"; // pointer to this line to use as zero in the C string ptr [n] = end; - a_s
  • static char const end [] = "end"; char ** ptr ... ptr [n] = end; will not work, because the types do not match each other. in the example, deleted 'const', but here, of course, also minuses, sometimes const is also needed. For all cases you can not do. - IAZ

Such arrays cannot know anything about their length. Alternatively, you can add one more element to the array, something like '\ 0' for the string.

PS '\ 0' is added to the end of the line to indicate its completion

    Do you have a mistake in the first code snippet?

     char ** ptr = new char*[n]; for (int i = 0; i < n; ++i) { ptr[i] = new char[m]; } 

    Then it turns out that ptr[i] is a pointer to the i-th row, and ptr[i][j] is a pointer to the j-th character of the i-th row.

    If the array is not sparse, then the lines (and the characters in them) can be iterated like this:

     int i = 0; while(ptr[i] != NULL) { char *curStr = prt[i]; // Текущая строка int j = 0; while(*currStr[j] != '\0') { char curChar = *currStr[j] // Текущий символ в строке ... j++; } i++; } 

    PS While writing the answer, you fixed the error)
    PSS I can not figure out how to format the code here ..

    • Understood, thanks. - Equinox
    • <pre> <code> some code </ code> </ pre> - AlexDenisov
    • @ 1101_debian Wrapping the code with tags is optional. Simply add the level of identification (four spaces). This can be done either in the source editor or in the input form (button 101010). - Nicolas Chabanovsky