简体   繁体   English

C函数指针混乱,什么是“变量的指针级别”?

[英]C function pointer confusion, what is meant by “pointer level of the variable”?

This is the code: 这是代码:

char *(*strcpy_ptr)(char *dst, const char *src); Pointer to strcpy-like function

And the tutorial says: 教程说:

Note the parentheses around *strcpy_ptr in the above declaration. 请注意上述声明中* strcpy_ptr的括号。 These separate the asterisk indicating return type (char *) from the asterisk indicating the pointer level of the variable (*strcpy_ptr — one level, pointer to function). 它们将指示返回类型(char *)的星号与指示变量的指针级别的星号(* strcpy_ptr —一级,指向函数的指针)分开。

I'm lost on this - where is the "function pointer" and what is the "pointer level" ? 我迷路了-“功能指针”在哪里,“指针级别”在哪里?

You are declaring a variable strcpy_ptr . 您正在声明一个变量strcpy_ptr You want this variable to be a pointer to a function returning a char* . 您希望此变量是指向返回char*的函数的指针。 If you did it without the parentheses this way: 如果您这样做是在没有括号的情况下进行的:

char **strcpy_ptr(char *dst, const char *src);

It would be the prototype of a function that returns a char** - not what you want. 这将是返回char**的函数的原型 -不是您想要的。 The parentheses are to group one star with the variable , and seperate the star from the return type . 圆括号将用变量将一颗恒星分组,并将该恒星与返回类型分开。

Remember that pointers are declared like this: 请记住,指针的声明是这样的:

T *var;

Where T is some type. 其中T是某种类型。 The more stars you add, the more levels of indirection you add before you finally get to the actual T . 添加的星星越多,在最终达到实际T之前添加的间接层越多。 So char **c would be a pointer to a pointer to a char . 因此char **c将是一个指向char的指针。 It's the same thing for function pointers: T is char* , and *var must be seperated by parentheses, because C is ignorant of white space. 对于函数指针,这是相同的: Tchar* ,而*var必须用括号分隔,因为C忽略空格。 C just added a little extra syntax to specify what kind of and how many arguments the function takes that is pointed to by the pointer. C刚刚添加了一些额外的语法来指定指针所指向的函数类型和参数数量。 This is just part of the way C works. 这只是C工作方式的一部分。

Without the parens, you would have: **strcpy_ptr 没有括号,您将拥有:** strcpy_ptr

This is a pointer to a pointer or double indirection. 这是指向指针或双向间接寻址的指针。 I think 'one level' means there is just one level of indirection. 我认为“一个级别”意味着只有一个间接级别。

@Adel: all most all parts of the above comments make some sense (with few picky exceptions like C doesn't understand white space), still I will suggest you to read "C programming language" by Kernighan and Ritchie, 2nd edition, chapter 5.11 and specially chapter 5.12 (Complicated expressions-p122): you will find good number of complicated examples and it is important to understand each one of them. @Adel:以上注释的所有大部分都是有意义的(有一些挑剔的异常,例如C不能理解空白),我仍然建议您阅读Kernighan和Ritchie撰写的“ C编程语言”,第二版,第二章5.11以及特别是第5.12章(复杂表达式-p122):您会发现很多复杂的示例,理解每个示例都很重要。

specially, understanding the differences between: 特别是,了解以下两者之间的区别:

  1. char (* (* x()) [] ) () 字符(*(* x())[])()

and

2.char (*(*x[])())[] 2.char(*(* x [])())[]

cforfun. cforfun。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM