如何解密C语言中的复杂指针声明? [英] How to decipher complex pointer declarations in C?

查看:64
本文介绍了如何解密C语言中的复杂指针声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想举个例子:

int *pi; // pi is a pointer that points to an integer
const int *cpi; // cpi is a pointer that points to a constant integer
char *pc; // pc is a pointer to a char 

我如何阅读这些内容:

char **x; //x is a pointer to a char pointer?
char *y[];
char **z[];

谢谢.

推荐答案

cdecl.org 通常被链接这样的问题.毫无疑问,它使解密任何复杂的声明变得更加容易,但与此同时,它仅提供了抽象的信息.作为一名C或C ++程序员,应该知道如何手动解密复杂的声明.螺旋规则有所帮助,但在某些

cdecl.org is often linked to such questions. No doubt that it make easier to decipher any complex declaration, but at the same time it just provide an abstracted information. Being a C or C++ programmer one should know how to decipher complex declaration manually. Spiral Rule help to some extent but fails in some cases. This answer will help programmers to decipher any complex declaration manually.

记住这两个简单的规则:

Remember these two simple rules:

  1. 始终从由内而外阅读声明.
  2. 如有选择,请始终优先使用 [] (),而不是 * .
  1. Always read declaration from the inside out.
  2. When there is a choice, always favor [] and () over *.

第一个规则简单地指出,找到要声明的变量,然后开始从中解密该声明.

The first rule simply states that, locate the variable that is being declared and start deciphering the declaration from it.

对于第二条规则,如果 * 在标识符之前,并且 [] ()在标识符之后,则标识符表示数组或函数(分别),而不是指针.

For second rule, if * precedes the identifier and [] or () follows it, then the identifier represents an array or function (respectively), not a pointer.

示例1:

char *y[5]; 

  • 变量/标识符为 y .
  • * y 之前,在 [] 之后.
  • y 必须是一个数组.
    • Variable/identifier is y.
    • * precedes y and follows [].
    • y must be an array.
    • 结合以上解密将得到: y 是指向 char 5 个指针的数组.

      Combining above deciphering will result in: y is an array of 5 pointers to char.

      还请注意,您始终可以使用括号来覆盖 [] ()的常规优先级.

      Also note that you can always use parentheses to override the normal priority of [] or ().

      示例2:

      void (*pf) (int);
      

      • 变量/标识符为 pf .
      • * pf 用括号括起来,它必须是一个指针.
      • () * pf 之后,表示 pf 必须指向一个函数.
      • 由于()包含了 int ,因此函数必须使用 int 类型的参数.
        • Variable/identifier is pf.
        • *pf is enclosed in parenthesis, it must be a pointer.
        • () follows *pf, means pf must points to a function.
        • Since () encloses int, function must expects an argument of type int.
        • 因此, pf 是指向函数的指针,该函数需要一个 int 参数并且不返回任何内容.

          So, pf is a pointer to function that expects an int argument and returns nothing.

          现在,破译以下声明后会得到什么

          Now, what would you get after deciphering the following declaration

          int *(*a[5])(void);  
          

          ?

          答案:

          a 是指向函数的指针数组,该函数不包含任何参数并返回指向 int 的指针.

          a is an array of pointers to functions that expects no argument and returning pointer to int.


          注意:请注意,

          char *y[];
          char **z[];  
          

          如果未将

          声明为函数的参数,则将导致编译错误.如果它们是函数的参数,则 char * y [] 等效于 char ** y ,而 char ** z [] 等效于 char *** z .
          如果不是这种情况,则需要像在第一个示例中一样指定尺寸.

          will cause compilation error if they are not declared as arguments of a function. If they are function's argument then char *y[] is equivalent to char **y and char **z[] is equivalent to char ***z.
          If that's not the case, then you need to specify the dimension as I did in my first example.

          这篇关于如何解密C语言中的复杂指针声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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