指针数组(对函数)感到困惑 [英] Confused by arrays of pointers (to functions)

查看:69
本文介绍了指针数组(对函数)感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Newb在这里所以如果可能的话,请尽量放心使用,非常感谢!)



我的C课本说(如果我理解正确的话):



类型 * pointername;

// 类型此处决定数据类型指针的目标



类型 * arrayname [SIZE];

// 类型这里也决定了数组中指针目标的数据类型......但现在数组元素的输入在哪里?



类型(* arrayname [SIZE])(typeofarguments);

// 类型现在决定返回值的类型数组中指针指向的函数??????? omg但现在在哪里输入指针目标和数组元素 ???



格式如何键入如此相似有如此截然不同的含义?我很困惑!非常感谢任何澄清!





另一个问题:顺便说一下,定义中是'*'指针仍然被视为运营商?如果没有,它究竟是什么?

(Newb here so please go easy on terming in your anwer if possible, many thanks!)

My C textbook says (if I understand it correctly):

type *pointername;
//type here decides the data type of the target of the pointer

type *arrayname[SIZE];
//type here also decides the data type of the targets of the pointers in the array ... but now where's the typing of the array elements?

type (*arrayname[SIZE])(typeofarguments);
//type now decides the type of returned values of function pointed by the pointers in the array??????? omg but now where are typing of pointer targets AND array elements???

How can type in formats so similar have so drastically different meanings? I'm so confused! Any clarification is greatly appreciated!


Another question: By the way, is '*' in definition of pointers still considered as an operator? If not, what is it exactly?

推荐答案

你的书并没有像你那样使用打字给你很多好处。声明的类型是声明中除名称之外的所有内容。所以当有人写道:



Your book's not doing you a lot of favours by using "type" the way it does. The type of a declaration is everything in a declaration apart from the name. So when someone writes:

int (*a[100])(int);





类型 a int(* [100])(int); 作者是说a是一个包含100个指针的数组,它将一个整数作为唯一参数并返回一个整数或a的类型是一个包含100个指针的数组,这些指针将一个整数作为其唯一参数并返回一个INT。数组中每个元素的类型是指向一个函数的指针,该函数接受一个整数作为它的唯一参数并返回一个整数。



通常你会使用 typedefs 使其更具可读性:





the type of a is int (*[100])(int); The author is saying "a is an array of 100 pointers to functions that take an integer as its only parameter and returns an integer" or "the type of a is an array of 100 pointers to functions that take an integer as its only parameter and returns an int". The type of each element in the array is "pointer to a function that takes an integer as it's only argument and returns an integer.

Normally you'd use typedefs to make this a bit more readable:

typedef int (*func_ptr)( int );

func_ptr a[100];



与第一个声明含义相同但告诉你在一个更容易阅读数组中每个元素的类型的形式,并且它更直接地显示 a 是一个数组。



阅读C声明的最佳报道我见过 Peter Van Der Linden 专家C编程。我d建议到亚马逊或你的本地图书馆,看看你是否可以得到一份副本 - 这是一个非常清晰的C的解析规则的帐户arations并解释得比我好得多。


which has the same meaning as the first declaration but tells you in a form that's a bit easier to read what the type of each element of the array is and it shows a lot more directly that a is an array.

The best coverage of reading C declarations I've seen is in Expert C Programming by Peter Van Der Linden. I'd recommend dashing to Amazon or your local library and seeing if you can get a copy - it's a really lucid account of C's parsing rules for declarations and explains it far better than I can.


http://cdecl.org/ [ ^ ]:您输入您的C声明并获得声明的prosa描述。



要阅读C声明,请执行以下迭代规则:

See http://cdecl.org/[^]: You enter your C-declaration and get "prosa" description of the declaration.

To read a C-declaration, do the following iterative rule:


  1. 从实体开始 名称

    - > 如果有 ... 名称声明为
  2. c $ c>到当前位置的右侧

    - > function(...)return
  3. 如果有 [ ... ] 到当前位置的右侧

    - > array ... of
  4. 如果 const volatile 到< b>左当前位置

    - > const或volatile
  5. 如果 * 到当前位置的左侧,则为

    - > 指向
  6. 的指针,如果当前位置嵌套在 ...

    - >没有
  7. 如果当前位置的左侧 typename ,则为
    />
    - > typename
  8. 如果所有吃完

    - >完成
  9. 否则转到2)

  1. start at the entity name
    --> "declare name as "
  2. if there is a ( ... ) to the right of the current location
    --> "function (...) returning "
  3. else if there is a [ ... ] to the right of the current location
    --> "array ... of "
  4. else if there is const or volatile to the left of the current location
    --> "const " or "volatile "
  5. else if there is * to the left of the current location
    --> "pointer to "
  6. else if the current location is nested in ( ... )
    --> nothing
  7. else if there is a typename to the left of the current location
    --> "typename "
  8. if all "eaten up"
    --> done
  9. else goto 2)





例如



E.g.

type *pointername;                        --> 1,5,9,7,8
type *arrayname[SIZE];                    --> 1,3,9,5,9,7,8
type (*arrayname[SIZE])(typeofarguments); --> 1,3,9,5,9,6,9,2,7,8



I.e.上述声明转换为:

- 将指针声明为|指向|类型

- 将arrayname声明为| |的数组大小指向|类型

- 将arrayname声明为| |的数组大小指向|函数(typeofarguments)返回|类型



干杯

Andi



PS:C ++多一点关于参考文献的复杂性(例如& && * <一起使用/ code>),未命名的实体(例如,在别名声明中类似于typedef:查找起始名称所在的位置),推断名称是类型还是类成员名称,指向成员的指针等好的是,人们可以很容易地扩展上述规则以适应C ++声明(或拒绝它们,因此它们是表达式)。例如。 X * Y;如果x是一个类型,则是一个声明,否则,它是一个表达式,其中不存储操作符调用的结果。


I.e. the above mentioned declarations translate into:
- declare pointername as | pointer to | type
- declare arrayname as | array SIZE of | pointer to | type
- declare arrayname as | array SIZE of | pointer to | function (typeofarguments) returning | type

Cheers
Andi

PS: C++ is a bit more complex with respect to references (e.g. & and && goes with *), unnamed entities (e.g. in alias declaration which are analogous to typedefs: find the location where the starting name would be located), deducing if a name is type or a class member name, pointer to member, etc. The good thing is, one can easily extend the above rules to fit C++ declaration (or to reject them so they are expressions). E.g. x*y; is a declaration if x is a type, otherwise, it is an expression where the result of the operator call is not stored.


好的,让我们考虑一下。

OK, lets think about this.
int i;

定义一个可验证的整数 - 它包含一个数字。

Defines an integer veriable - it holds a number.

int *pi;

定义指向可验证整数的指针 - 它保存数字的地址。

Defines a pointer to an integer veriable - it holds the address of a number.

int ai[10];

定义一个整数数组 - 它包含一系列数字。

Defines an array of integers - it holds a sequence of numbers.

int *api[10];

定义一个指向整数的指针数组 - 它包含一系列指向数字的指针。



类型定义您将使用的最终类型的变量:当您取消引用变量时,决定哪个数组元件你正在处理(如果有的话)你将能够投入和取出变量。

所以这些都是有效的:

Defines an array of pointers to integers - it holds a sequence of pointers to numbers.

The type defines the eventual type of variable you will be working with: when you dereference the variable, and decide which array element you are dealing with (if any) that is what you will be able to put into and take out of the variable.
So all these are valid:

i = 666;
*pi = 666;
ai[3] = 666;
*ai[3] = 666;



使用指向整数的指针数组,类型 int ,*表示这是指针和是定义数组元素实际类型的附加信息。


With an array of pointers to integers, the type is int, the "*" says "this is pointers to" and is additional information that defines the actual type of the array elements.


这篇关于指针数组(对函数)感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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