我不明白以下指针变量声明在 c 中的含义

发布于 2024-10-06 07:14:59 字数 186 浏览 4 评论 0原文

  1. char(*p)[15];
  2. char(*p)(int *a);
  3. int(*pt)(char*); >
  4. int *pt(char*);

有人帮忙吗?

  1. char(*p)[15];
  2. char(*p)(int *a);
  3. int(*pt)(char*);
  4. int *pt(char*);

anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自我难过 2024-10-13 07:14:59

基本规则:从标识符开始,可以向右阅读,必须向左阅读。

  1. 从标识符*开始。说出来,然后是“是”。将您的“左脚”放在其左侧一个字符。
  2. 向右阅读,直到读到末尾或 )。将你的“右脚”放在 ) 所在位置的右侧一个字符(如果这就是你击中的位置)。
    • 如果向右阅读时看到 [42],请说“array of 42”。
    • 如果您在向右阅读时看到 (,请说“函数获取”,然后递归说出每个参数的类型(但省略参数名称本身),然后通过“并返回”。
  3. 现在跳到你的左脚上并向左阅读,直到你击中开始或 (。如果是的话,将你的左脚放在 ( 左边一个字符你击中了。
    • 如果向左阅读时看到 *&,请说“指向”或“引用”。
    • 您看到的任何其他内容(例如 constintMyFoo),只需说出来即可。
  4. 如果你点击了开始,你就完成了。否则,跳回右脚并转到 2。

* 如果没有标识符,想象它必须去哪里——我知道这很棘手,但只有一个合法的位置。

遵循以下规则:

  1. p 是一个指向 15 个字符的数组的指针
  2. p 是一个指向函数的指针,该
  3. 函数接受指向 int 的指针并返回 char pt 是一个指向函数的指针,该函数接受指向 char 的指针并返回 int
  4. pt 是一个接受指向 char 的指针并返回指针的函数到整数

Basic rule: Start at the identifier and read right when you can, left when you must.

  1. Start at the identifier*. Say it, followed by "is a". Put your "left foot" one character to the left of it.
  2. Read rightwards until you hit the end or a ). Put your "right foot" one character to the right of where that ) is, if that's what you hit.
    • If you see [42] as you read rightwards, say "array of 42".
    • If you see a ( as you read rightwards, say "function taking", then recurse to say each parameter's type (but omit the parameter names themselves), followed by "and returning".
  3. Now hop onto your left foot and read leftwards until you hit the start or a (. Put your left foot one character to the left of the ( if that's what you hit.
    • If you see a * or a & as you read leftwards, say "pointer to" or "reference to".
    • Anything else you see (e.g. const, int, MyFoo), just say it.
  4. If you hit the start, you're done. Otherwise, hop back onto your right foot and goto 2.

* If there is no identifier, imagine where it must go -- tricky I know, but there's only one legal placement.

Following these rules:

  1. p is a pointer to array of 15 char
  2. p is a pointer to function taking pointer to int and returning char
  3. pt is a pointer to function taking pointer to char and returning int
  4. pt is a function taking pointer to char and returning pointer to int
疧_╮線 2024-10-13 07:14:59

找出指针指向什么类型的一个简单技巧是删除 * 并查看剩下的内容:

  1. char p[15];
  2. char p(int *a);
  3. int pt(char*);
  4. int pt(char*);

您得到的是指针将指向的类型的变量声明到。或者不是第四种情况:

int *pt(char*);

是函数原型而不是有效的指针声明。

编辑:

原因是没有括号,函数调用“运算符”优先于指针取消引用运算符。在上面的例子中,简单英语的声明是:

我们有一个 pt(char *) 函数,它返回一个 int *

int (*pt)(char *);

翻译为:

*pt 是一个接受 char * 并返回 int 的函数。

这本质上意味着 pt 本身就是指向该类型的指针。

A simple trick to find out what type a pointer points to is to just remove the * and see what's left:

  1. char p[15];
  2. char p(int *a);
  3. int pt(char*);
  4. int pt(char*);

What you get is a variable declaration of the type your pointer will point to. Or not in the fourth case:

int *pt(char*);

is a function prototype and not a valid pointer declaration.

EDIT:

The reason is that without the parentheses, the function call "operator" takes precedence over the pointer dereference operator. In the case above, the declaration in plain English is:

We have a pt(char *) function, which returns an int *

While

int (*pt)(char *);

translates as:

*pt is a function that takes a char * and returns an int.

Which essentially means that pt on its own is a pointer to that type.

荭秂 2024-10-13 07:14:59
  1. 指向十五个字符的数组的指针。
  2. 函数指针。接受 int 指针并返回 char
  3. 与 4 函数原型相同
  4. 。获取 char 并返回 int。
  1. Pointer to array of fifteen chars.
  2. Function pointer. Takes int pointer and returns char
  3. Same as 4
  4. Function prototype. Takes char and returns int.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文