写&QUOT好办法;指针的东西"在C / C ++ [英] good way to write "pointer to something" in C/C++

查看:115
本文介绍了写&QUOT好办法;指针的东西"在C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个好的方式来写指针的东西,在C / C ++?结果
我用写的无效美孚(字符* STR); 但有时我觉得很不合逻辑,因为 STR 的类型是字符指针,那么就应该更符合逻辑附加 * 来的类型名称。结果
有没有写指针的规则?

 的char * str中;
字符*海峡;
字符*海峡;
字符*海峡;


解决方案

常见的C约定是写 T * P ,而常见的C ++的惯例是写 T * p 。这两种解析为 T(* P);在 * 是说明符,而不是类型说明符的一部分。这是纯粹的指针声明语法的事故,你可以写任何一种方式。

C(通过扩展,C ++)的声明语法的前pression中心的; IOW,声明的形式应该与code同类型的前pression的形式。

例如,假设我们有一个指针 INT ,我们想访问的整数值。要做到这一点,我们的解引用的指针与 * 间接运算符,就像这样:

  X = * P;

该类型的前pression的 * P INT ;因此,它遵循 P 的声明应

  INT * P

P 由类型说明符所提供的int-内斯 INT ,但指针岬 p 由声明符提供的 * p

作为一个稍微复杂一点的例子,假设我们有一个指针浮动,数组,并希望在访问浮点值'日通过该指针数组的元素。我们取消引用指针数组下标结果:

  F =(* AP)[我]

该类型的前pression的(* AP)[我] 浮动,所以它遵循数组指针的声明是

 浮动(* AP)[N];

AP的浮动岬通过类型说明符浮动,但指针的烦躁和阵列提供-ness由声明符提供的(* AP)[N] 。请注意,在这种情况下, * 必须的明确的绑定到标识符; [] 具有更高的precedence比一元 * 这两个前pression和声明的语法,所以浮动* AP [N] 将被解析为浮动*(AP [N])或指针数组到浮动,而不是指针到浮动的数组。我想你的可能的写为

 浮动(* AP)[N];

但我不知道该点会是什么;它不会使 AP更清楚的类型。

更妙的是,怎么样一个指向返回一个指针,指针数组给一个函数 INT

 为int *(*(* F)())[N];

此外,至少两个的 * 运营商必须明确的声明符的约束;结合最近的 * 的类型说明符,如

 为int *(*(* F)())[N];

只是表明困惑IMO思考。

虽然我用它在我自己的C ++ code,即使我明白为什么走红,我与 T * P背后的原因问题的约定是,它的只是不适用的最简单的指针声明的外面,它强化一个简单到最点的存在,错误的观点C和C ++声明语法。是的,类型 P 是指向T,但这并不改变的事实,至于语言的语法来讲 * 绑定到声明符,而不是类型说明符。

有关另一种情况,如果类型 A 是T N个元素的数组,我们的的写

  T [N]一个;

显然,语法不允许它。同样,参数的只是没有在这种情况下适用的。

修改

由于史蒂夫在评论中指出的那样,你可以使用typedef来隐藏某些复杂性。例如,您可以重写

 为int *(*(* F)())[N];

作为像

 的typedef为int * iptrarr [N]; // iptrarr是指针数组为int
typedef的iptrarr * arrptrfunc(); // arrptrfunc是返回一个函数
                                   //指针iptrarrarrptrfunc * F; // f是到arrptrfunc指针

现在的,你可以清晰地适用 T * P 约定,宣告˚F作为 arrptrfunc *˚F。我个人不喜欢做的事情这样的,因为它不是从类型定义如何˚F应该在离pression,或将使用怎样不一定清楚使用键入的对象arrptrfunc 。非typedef定义版本,可能是丑陋,难以阅读,但至少它告诉你你需要知道前面的一切;你不必去通过所有的typedef挖。

Is there a "good" way to write "pointer to something" in C/C++ ?
I use to write void foo( char *str ); But sometimes I find it quite illogical because the type of str is "pointer to char", then it should more logical to attach the * to the type name.
Is there a rule to write pointers ?

char*str;
char* str;
char *str;
char * str;

解决方案

The common C convention is to write T *p, whereas the common C++ convention is to write T* p. Both parse as T (*p); the * is part of the declarator, not the type specifier. It's purely an accident of pointer declaration syntax that you can write it either way.

C (and by extension, C++) declaration syntax is expression-centric; IOW, the form of a declaration should match the form of an expression of the same type in the code.

For example, suppose we had a pointer to int, and we wanted to access that integer value. To do so, we dereference the pointer with the * indirection operator, like so:

x = *p; 

The type of the expression *p is int; thus, it follows that the declaration of p should be

int *p  

The int-ness of p is provided by the type specifier int, but the pointer-ness of p is provided by the declarator *p.

As a slightly more complicated example, suppose we had a pointer to an array of float, and wanted to access the floating point value at the i'th element of the array through the pointer. We dereference the array pointer and subscript the result:

f = (*ap)[i];

The type of the expression (*ap)[i] is float, so it follows that the declaration of the array pointer is

float (*ap)[N];

The float-ness of ap is provided by the type specifier float, but the pointer-ness and array-ness are provided by the declarator (*ap)[N]. Note that in this case the * must explicitly be bound to the identifer; [] has a higher precedence than unary * in both expression and declaration syntax, so float* ap[N] would be parsed as float *(ap[N]), or "array of pointers to float", rather than "pointer to array of float". I suppose you could write that as

float(* ap)[N];

but I'm not sure what the point would be; it doesn't make the type of ap any clearer.

Even better, how about a pointer to a function that returns a pointer to an array of pointer to int:

int *(*(*f)())[N];

Again, at least two of the * operators must explicitly be bound in the declarator; binding the last * to the type specifier, as in

int* (*(*f)())[N];

just indicates confused thinking IMO.

Even though I use it in my own C++ code, and even though I understand why it became popular, the problem I have with the reasoning behind the T* p convention is that it just doesn't apply outside of the simplest of pointer declarations, and it reinforces a simplistic-to-the-point-of-being-wrong view of C and C++ declaration syntax. Yes, the type of p is "pointer to T", but that doesn't change the fact that as far as the language grammar is concerned * binds to the declarator, not the type specifier.

For another case, if the type of a is "N-element array of T", we don't write

T[N] a;

Obviously, the grammar doesn't allow it. Again, the argument just doesn't apply in this case.

EDIT

As Steve points out in the comments, you can use typedefs to hide some of the complexity. For example, you could rewrite

int *(*(*f)())[N];

as something like

typedef int *iptrarr[N];           // iptrarr is an array of pointer to int
typedef iptrarr *arrptrfunc();     // arrptrfunc is a function returning
                                   // a pointer to iptrarr

arrptrfunc *f;                     // f is a pointer to arrptrfunc

Now you can cleanly apply the T* p convention, declaring f as arrptrfunc* f. I personally am not fond of doing things this way, since it's not necessarily clear from the typedef how f is supposed to be used in an expression, or how to use an object of type arrptrfunc. The non-typedef'd version may be ugly and difficult to read, but at least it tells you everything you need to know up front; you don't have to go digging through all the typedefs.

这篇关于写&QUOT好办法;指针的东西"在C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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