C ++: function arg char ** does not match char * []

I am using g ++. I am using code with main(int,char**) renamed, so I can call it. I looked at Should I use char ** argv or char * argv [] in C? where char** is considered equivalent to char* [] . This is not like making calls to C ++ functions. For instance:

 void f1(char** p){;} void f2(char* p[]){ f1(p); //...` } 

the compiler cannot compile โ€œcannot convert char (*)[] to char** ...โ€ The links that I want to say are that arrays are converted to pointers for invocation, but this does not seem to be the case:

 void f3(char* [] p); char caa[16][16]; f3(caa); 

also fails. I assumed that as long as the levels of indirection are the same (for example, char*** ptr and char[][][] carray ), the types were interchangeable.

Can someone provide a link that I can view that clarifies these issues?

Thanks.

+4
source share
2 answers

This is still persisted in C ++. If your compiler complains as you describe for your first case, it is inappropriate.

To explain your second case, it is important to understand what is actually happening. An array type expression is implicitly converted to the corresponding pointer type, that is, T[n] โ†’ T* . However, if T itself is an array, this case is not handled specifically, and the decomposition between arrays and pointers does not extend. Thus, T*[n] splits into T** , but T[x][y] will only fade to T[y]* and no further.

From an implementation point of view, this makes sense, because further decay, if allowed, will produce T** , which is a pointer to a pointer; whereas 2D C arrays are not implemented as jagged arrays (i.e., an array of pointers to arrays), they form a single continuous block of memory. So, there is no T* "inside" of the array to accept the address to give you T** . For permitted cases, a typical implementation simply takes the address of the array as a whole and converts it to a pointer type on one element (when the basic representation of the pointer is the same for all types, as it usually happens, this conversion is no-op at run time).

The normative reference here is ISO C ++ 03, 4.2 [conv.array] / 1:

The value of an lvalue or rvalue of type "array NT" or "array of unknown boundary T" can be converted to rvalue type "pointer to T". The result is a pointer to the first element of the array.

+12
source
 void f2(char* p[]){ 

the compiler complains " cannot convert char (*)[] to char**... "

Itโ€™s strange. char(*)[] is a pointer to an array of characters , but in your code fragment, the function has an argument char *p[] , which means an array of pointers to char ! These types are really different (because the elements of the array have different sizes), not to mention the fact that your code fragment compiles fine. You really saddened something.

Sherlock Holmes mode: or is there a typedef involved ?; -)

 void f1(char** p){;} typedef char type[]; void f2(type * p){ f1(p); } 

It really does not compile and does not give the error you were talking about.

+1
source

Source: https://habr.com/ru/post/909827/


All Articles