I will just drop a short paragraph of code, it will be clearer than words:

int main() { int n; int i; char *p; char names[N][M-1]; scanf(«%d», &n); for (i=0; i<n; i++) scanf(«%s», names[i]); /**********************************************/ ****/* NEED TO DEFINE AND INITIALIZE HERE names_p */**** /**********************************************/ p = (char *) get_all_names(names_p, n); { if (p != NULL) { printf(p); free(p); } return 0; } char *get_all_names(char *names[], int n) 

You need to pass names to get_all_names with the help of names_p , which you actually need to define as a readable name for the function being passed.

I don’t understand how to define names_p so that the get_all_names function get_all_names accept it, despite the fact that its first argument is of type [[], (and the names itself are of type [] []).

In this example I will be glad to briefly explain the difference between these types and here the necessary castings.

PS - I work in C version 99, but it is desirable that it works on 89 too.

The condition of the task: you can not change anything, you only need to execute a comment written in English (identify and initialize in a specific line)

Closed due to the fact that the essence of the issue is incomprehensible by the participants Alexei Averchenko , user6550, Athari , Alexey Shtanko , PashaPash ♦ Jun 12 '15 at 17:55 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • @klopp - Well, why are you so? Two-dimensional arrays in C are confusing. Sometimes a two-dimensional array is created as a "through" one-dimensional (running through all the rows of the matrix in a row), and sometimes as an array of pointers to one-dimensional arrays. And then one to the other will not lead the usual caste. - Egor Skriptunoff
  • I vote for the closure of this issue as not relevant to the topic, because this is a trivial learning task with artificial restriction - PashaPash ♦

2 answers 2

In general, it is better to emulate two-dimensionality, creating a one-dimensional array of size NxN, and the [i] [j] element will be here [(i * N) + j]

  • one
    Firstly, in this case, the whole essence of the task lies precisely in combining the initially physically different interfaces. Those. Interface specifications go down to you "above" and are not negotiable. Secondly, it is better or not better depends on the specifics of the application. "Torn arrays" occupy their niche. Emulation through one-dimensional - your own. - AnT

You need to create an "index" array of pointers required by the get_all_names function

 char *names_p[N]; ... for (i = 0; i < n; ++i) names_p[i] = names[i]; p = get_all_names(names_p, n); 

Everything.

What "difference explanation" you wish to receive is not entirely clear. Treatises do not want to write (read the literature), so I will be brief.

  1. [][] is a classic "embedded" two-dimensional array of C, i.e. flat array of arrays. The memory is represented as a flat continuous block, whose size is equal to the product of the two specified sizes.

  2. *[] is an array of pointers. Since address arithmetic of the C language makes it possible to treat a pointer as a pointer to the beginning of a certain array, such a data structure can act as an alternative implementation of a two-dimensional array. Responsibility for initializing pointers rests with you. Externally, the syntax of working with such an array is the same as [][] , but the internal structure of such an array is fundamentally different from 1 and not compatible with 1.

    This way of representing a two-dimensional array — an array of pointers to subarrays — is known as a “jagged array” or “ragged array”.

  3. ** - pointer to pointer. Again, for the same reasons, such a pointer to a pointer can be used to implement a two-dimensional array. Again, the syntax for working with such an array is the same as [][] , but the internal structure of such an array is fundamentally different from 1 and not compatible with 1.

That's precisely because of the incompatibility of 1 and 2, and you need to write additional code that converts your names (type 1) to names_p (type 2).


PS By the way, in honor of what it’s written to you is the casting of the result type get_all_names to type (char *) if get_all_names and so returns char * ? I suspect that you call the undeclared function get_all_names and, in order to suppress the warning from the compiler, you decided to put a type conversion there? For this - immediately shot. Therefore, about any "you can not change anything" is out of the question. Either move the definition of get_all_names top, or add the declaration get_all_names at the top.