复杂的C声明
回答
我还没有在一段时间做到了这一点!
以foo
开头并右转。
float * (*(*
foo()
)[SIZE][SIZE])()
foo是不带参数的函数...
不能右转,因为有一个右括号。向左走:
float * (*(
* foo()
)[SIZE][SIZE])()
foo是不带任何参数返回一个指针
不能去再留下一个功能,让我们穿越括号便又去
float * (*
(* foo())
[SIZE][SIZE])()
float * (*
(* foo())[SIZE]
[SIZE])()
float * (*
(* foo())[SIZE][SIZE]
)()
foo是不带参数返回一个指针到SIZE的SIZE数组的数组的函数...
闭括号达到,再次离开以达到指针符号:
float * (
*(* foo())[SIZE][SIZE]
)()
foo是不带任何参数返回一个指向大小为指针的大小数组的数组的功能...再次
左括号,所以我们超越了这去便又:
float *
(*(* foo())[SIZE][SIZE])
()
float *
(*(* foo())[SIZE][SIZE])()
foo是不带参数返回一个指针到SIZE指针SIZE数组的数组,以不带参数的功能的功能...
并留给端
float * (*(* foo())[SIZE][SIZE])()
foo是不带任何参数返回一个指向大小为指针的大小数组的数组不带参数的返回指针的函数等漂浮
和谁写的,请教他用typedef
:
// Function that returns a pointer to float
typedef float* PFloatFunc();
// Array of pointers to PFloatFunc functions
typedef PFloatFunc* PFloatFuncArray2D[SIZE][SIZE];
// Function that returns a pointer to a PFloatFuncArray2D
PFloatFuncArray2D* foo();
+1代表“谁写了这些,p租赁教他使用typedef“ – 2013-02-27 14:31:36
请注意,”无参数“部分仅适用于C++;对于C来说,它意味着“未指定的参数列表”(但它不能是可变参数列表,因为它们必须具有完整的原型范围,即使在C中也是如此)。 – 2013-03-06 01:08:08
一般情况下,你可以尝试cdecl.org但你需要替换SIZE
说你换SIZE
12,你会得到:
声明富作为函数返回指针数组12 指针函数返回指向浮点数的函数
我不确定这对你真的有帮助!
两个结论:这里:
- 我猜测,这个代码没有它解释什么它的目的是(旁边有一个评论即不知道是什么它就是什么,但在技术说明从功能/业务角度实现)如果程序员需要使用像这样复杂的东西,他们应该足以向未来的维护者解释其服务目的。
- 当然,在C++中,实现同样的事情有更明显的和更安全的方法。
这是由于“SIZE”的原因,你必须使用literal来代替它(你自己用常量代替它) 。 – 2013-02-27 12:02:32
用一些数字代替SIZE !! – Kaunteya 2013-02-27 12:04:46
回答更改 - 感谢您的反馈。 – 2013-02-27 12:09:42
声明富作为函数返回指针,指针数组大小的数组大小,以函数返回指针浮
这里要做的最好的事情是转换为一系列typedefs。
typedef float * fnReturningPointerToFloat();
typedef fnReturningPointerToFloat* fnArray[SIZE][SIZE];
fnArray* foo();
这花了我一分钟。 – QuentinUK 2013-02-27 12:24:18
标准规则:找到最左边的标识和工作的方式了,记住,[]
和()
绑定之前*
:
foo -- foo
foo() -- is a function
*foo() -- returning a pointer
(*foo())[SIZE] -- to a SIZE-element array
(*foo())[SIZE][SIZE] -- of SIZE-element arrays
*(*foo())[SIZE][SIZE] -- of pointers
(*(*foo())[SIZE][SIZE])() -- to functions
* (*(*foo())[SIZE][SIZE])() -- returning pointers
float * (*(*foo())[SIZE][SIZE])(); -- to float
所以,想象一下你有一大堆的返回指针的函数来float
:
float *quux();
float *bar();
float *bletch();
float *blurga();
假设您想将它们存储在2x2表中:
float *(*tab[SIZE][SIZE])() = {quux, bar, bletch, blurga};
tab
是指向返回指向float
的指针的函数指针的SIZE x SIZE数组。
现在让我们来决定我们想要一个函数返回一个指向该表:
float *(*(*foo())[SIZE][SIZE])()
{
static float *(*tab[SIZE][SIZE])() = {quux, bar, bletch, blurga};
return &tab;
}
需要注意的是,你可以有一个建立不同的功能表中的几个功能,或不同的方式组织相同的功能:
float *(*(*qwerbl())[SIZE][SIZE])()
{
static float *(*tab[SIZE][SIZE])() = {blurga, bletch, bar, quux};
return tab;
}
这是我能想到做这种事情的唯一原因。你不应该经常在野外看到这种类型的东西(尽管偶尔会出现这种情况,而且我曾经写过类似的令人发指的东西)。
qwerbl?您几乎用完了泛型变量名称,是不是:-) +1的基本原理。我确定“深度相关”类型经常出现,但通常也会涉及到结构或类,这就使得命名问题自然消失 - 就像引入某种类型定义时的情况一样。 – Kos 2013-02-27 12:39:33
@Kos:是的。还没有我的咖啡因的RDA,不能拿出更好的东西。 – 2013-02-27 14:11:12
维基百科有一个[metasyntactic变量列表](http://en.wikipedia.org/wiki/Metasyntactic_variable#English),这样你就不会跑出来:富,酒吧,巴兹,qux,QUUX,corge,grault,garply ,waldo,fred,plugh,xyzzy,thud。 – 2013-02-27 17:08:17
本文gaves我关于最好的线索,如何轻松地准备任何C声明:
http://c-faq.com/decl/spiral.anderson.html
有三个简单的步骤如下:
与未知的开始元素,以螺旋/顺时针方向移动; ecountering以下元素时与相应的英文语句代替它:
[X]
或...或数组的大小未定义=[]
>阵列X大小...
(type1, type2)
=>功能通过type1和类型2恢复...
*
=>指针(多个)为...继续以螺旋/顺时针方向进行操作,直到所有标记都被覆盖。
总是先解决括号中的任何内容吧!
实施例:
+-------+
| +-+ |
|^| |
char *str[10];
^^ | |
| +---+ |
+-----------+
Question we ask ourselves: What is str?
``str is an...
- We move in a spiral clockwise direction starting with `str' and the first character we see is a `[' so, that means we have an array, so...
``str is an array 10 of...
- Continue in a spiral clockwise direction, and the next thing we encounter is the `*' so, that means we have pointers, so...
``str is an array 10 of pointers to...
- Continue in a spiral direction and we see the end of the line (the `;'), so keep going and we get to the type `char', so...
``str is an array 10 of pointers to char''
We have now ``visited'' every token; therefore we are done!
- 1. 复杂声明
- 2. 将复杂的声明,()
- 3. FlowType - 重复复杂对象的声明
- 4. 复杂的群体,由MySQL声明
- 5. AspectJ的复杂层次声明父母
- 6. 了解这个复杂的声明
- 7. 有点复杂的MySQL声明
- 8. 理解复杂的迅速associatedtype声明
- 9. 复杂的SQL更新声明
- 10. C到C++转换杂项声明
- 11. 如何阅读这个复杂的C声明?
- 12. 使用日期复杂countifs声明
- 13. SQL UPDATE复杂选择查询声明
- 14. 复杂性证明
- 15. 在C++中重复声明的问题
- 16. Typedef和C中的复合声明
- 17. 我怎样才能使这个LINQ的声明更复杂的
- 18. C - 声明中的未声明变量
- 19. CVXOPT安装失败,在复杂的类型声明文件misc.h
- 20. 如何解码指针+数组的复杂声明?
- 21. Laravel4原始声明在复杂的中间
- 22. Javascript声明一个具有复杂JSON格式的变量
- 23. .NET Framework中的复杂声明值与System.Security.Claims
- 24. 用Java中的复杂类声明泛型变量
- 25. 复杂的地方/加入声明与codeigniter跨几个表
- 26. 更多可读写复杂的方法如果声明
- 27. 在python中声明复杂数据结构的类型
- 28. 以下声明的复杂程度如何?
- 29. C++的case声明?
- 30. C++ typedef声明
[螺旋规则](http://c-faq.com/decl/spiral。anderson.html) – 2013-02-27 11:58:18
http://cdecl.org/ – juanchopanza 2013-02-27 11:59:59
使用http://cdecl.org/ – 999k 2013-02-27 12:00:06