找出C声明,例如:double(* b)[n] [英] Figuring out C Declarations like: double (*b)[n]

查看:71
本文介绍了找出C声明,例如:double(* b)[n]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出一些C声明。这些C声明的含义是什么?

I'm trying to figure out some C declarations. What is the meaning of these C declarations?

double (*b)[n];
double (*c[n])();
double (*d())[n];


推荐答案

double (*b)[n];

b是指向n个双精度数组的指针

b is a pointer to an array of n doubles

double (*c[n])();

c是由n个指针组成的数组,这些函数指向未指定数量的参数并返回double的函数

c is an array of n pointers to functions taking unspecified number of arguments and returning double

double (*d())[n];

d是一个函数,它接收未指定数量的参数并返回指向n个双精度数组的指针

d is a function taking unspecified number of arguments and returning a pointer to an array of n doubles

通常,为了解析这种类型的声明,请采用以下方法。让我们看一下最后一个声明,例如

In general, in order to parse these kind of declarations in your head, take the following approach. Let's see the last declaration, for example

 double (*d())[n];

对d做的第一件事是什么?它用()调用,因此这是一个函数,它接收未指定数量的参数和returnig ...结果如何处理?已取消引用(*),因此它是指向的指针。然后将结果编入索引,因此它是 n个数组 ...还剩下什么?双,因此为

what is the first thing that is done to d? It is called with (), therefore it's a function taking unspecified number of arguments and returnig... what's the thing done with the result? It is dereferenced (*), therefore it's a pointer to. The result is then indexed, therefore it's an array of n... what's left? a double, therefore of doubles. Reading the parts in bold, you'll get your answer.

让我们看看另一个示例

 void (*(*f)(int)[n])(char)

在这里,首先 f 被取消引用,因此它是指向的指针...然后使用(int)进行调用,因此函数采用int并返回,然后以[n]为结果建立索引,因此 n的数组。结果再次被取消引用,因此指向。然后,结果由(char)调用,因此函数采用char 并返回(所有剩余为空) void 。因此,f是指向采用int并返回n个指针数组的指针,该数组指向采用char并返回void的函数

Here, f is first dereferenced, therefore it's a pointer to... it's then called with (int), therefore a function taking int and returning , the result is then indexed with [n], so an array of n. The result is dereferenced again, so pointers to. Then the result is called by (char), so functions taking char and returning (all is left is void) void. So f is a pointer to a function taking int and returning an array of n pointers to functions taking char and returning void.

HTH

这篇关于找出C声明,例如:double(* b)[n]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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