c ++:function arg char **不同于char * [] [英] c++: function arg char** is not the same as char*[]

查看:254
本文介绍了c ++:function arg char **不同于char * []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用g ++。我使用的代码,有一个 main(int,char **),重命名,所以我可以调用它。我查看了 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(*)[] 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);

也会失败。我假设只要间接级别相同(例如 char *** 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.

为了解释你的第二种情况,重要的是要了解实际发生了什么。数组类型的表达式可隐式转换为相应的指针类型,即: 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 ** ,这是指针的指针;而2D C阵列不实现为锯齿状阵列(即,指向阵列的指针阵列) - 它们形成单个连续的存储器块。所以,没有 T * 里面数组取地址给你一个 T ** 。对于允许的情况,典型的实现只是将数组的地址作为一个整体,并将其转换为指向单个元素的指针类型(当底层指针表示对于所有类型都是相同的,通常情况下,这种转换是一个

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的指针的右值。 result是指向数组的第一个元素的指针。

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 ++:function arg char **不同于char * []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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