C ++:功能ARG CHAR **是不一样的char * [] [英] c++: function arg char** is not the same as char*[]

查看:197
本文介绍了C ++:功能ARG CHAR **是不一样的char * []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的G ++。我使用code,它有一个主(INT,CHAR **),重命名,因此我可以调用它。我看了看<一个href=\"http://stackoverflow.com/questions/779910/should-i-use-char-argv-or-char-argv-in-c\">http://stackoverflow.com/questions/779910/should-i-use-char-argv-or-char-argv-in-c,其中,的char ** 据说是相当于的char * [] 。这似乎不是在C ++函数调用真实。例如:

I am using g++. I am using code that had a main(int,char**), renamed so I can call it. I looked at http://stackoverflow.com/questions/779910/should-i-use-char-argv-or-char-argv-in-c, where char** is said to be equivalent to char* []. This does not appear to be true in c++ function calls. For example:

void f1(char** p){;}

void f2(char* p[]){

   f1(p);

 //...`

}

失败,编译器抱怨无法转换字符(*)[] 的char ** ... 在引用我期待地说,数组转换为呼叫三分球,但是这似乎并没有这样的情况为:

fails with the compiler complaining "cannot convert char (*)[] to char**..." The references I look to say that arrays are converted to pointers for the call, but this does not seem to be the case as:

void f3(char* [] p);

char caa[16][16];  
f3(caa);

也失败。我曾以为,只要间接的水平是相同的(例如字符*** PTR 的char [] [] [] CARRAY )的类型是可以互换的。

also fails. I had assumed that as long as the levels of indirection were the same (e.g. char*** ptr and char[][][] carray ) the types were interchangeable.

有人可以提供一个参考,我可以审查澄清这些问题?

Can someone provide a reference I can review that clarifies these issues?

感谢。

推荐答案

这还是用C也是如此++。如果你的编译器抱怨你描述你的第一个案例,这是不符合的。

This still holds true in C++. If your compiler complains as you describe for your first case, it is non-conformant.

要解释你的第二个案例,了解实际发生的事情是重要的。数组类型的前pression隐式转换为相应的指针类型,即: T [N] - > T * 。但是,如果 T 本身是一个数组,这种情况下不经过特殊处理的,和阵列到指针衰减的不传播的。因此, T * [N] 衰减到 T ** ,但 T [X] [ Y] 只会衰减到 T [Y] * ,并没有进一步。

To explain your second case, it is important to understand what actually happens. An expression of array type is implicitly convertible to a corresponding pointer type, i.e.: T[n] -> T*. However, if T itself is an array, this case isn't treated specially, and array-to-pointer decay does not propagate. So T*[n] decays to T**, but T[x][y] will only decay to T[y]*, and no further.

从实现的角度来看,这是有道理的,因为腐烂的进一步,如果允许的话,会给 T ** ,这是指针的指针;而二维C数组并不像锯齿数组实现的(即指针数组的数组) - 它们形成一个连续的内存块。所以,没有 T * 内部拿的地址给你一个 T ** 阵列。对于允许的情况下,一个典型的实现只是将数组作为一个整体的地址,并将其转换为类型的指针,以单一元素(基本指针重新presentation时,是所有类型一样的,通常情况下,这是皈依在运行时无操作)。

From implementation perspective this makes sense, because decaying further, if allowed, would give T**, which is pointer to pointer; whereas 2D C arrays aren't implemented as jagged arrays (i.e. array of pointers to arrays) - they form a single contiguous memory block. So, there's no T* "inside" the array to take an address of to give you a T**. For the allowed cases, a typical implementation simply takes the address of the array as a whole and converts it to type of pointer to single element (when underlying pointer representation is the same for all types, as is usually the case, this convertion is a no-op at run time).

这里的引用标准是ISO C ++ 03,4.2 [conv.array] / 1:

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

这是左值或类型数组的NT或结合的T未知的阵列的右值可以被转换成类型的指针T.右值的结果是一个指针数组的第一个元素。

An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be converted to an rvalue of type "pointer to T." The result is a pointer to the first element of the array.

这篇关于C ++:功能ARG CHAR **是不一样的char * []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆