Ale*_*lex 4 c arrays function-pointers multidimensional-array
...不使用typedef.
我的老板声称他曾经在接受采访时被问过,当他回答时,采访者告诉他他不能使用typedef因为风格很差.
无论如何,他喜欢向人们提出问题只是为了看看他们是否能够做到正确,通常是在新的程序员午餐时间.没有人能做对(特别是没有笔和纸或电脑).我希望下次他试图用它来阻止某人时做好准备>:D
二维数组的指针是什么?
T *p[N][M]; // N-element array of M-element array of pointer to T
T (*p[N][M])(); // N-element array of M-element array of pointer to
// function returning T
Run Code Online (Sandbox Code Playgroud)
如果我们谈论的是指向2D数组的指针,那么事情只会变得更有趣:
T a[N][M]; // N-element array of M-element array of T
T (*p)[M] = a; // Pointer to M-element array of T
T (**p2)[M] = &p; // Pointer to pointer to M-element array of T
T (*p3)[N][M] = &a; // Pointer to N-element array of M-element
// array of T
T (**p4)[N][M] = &p3; // Pointer to pointer to N-element array of
// M-element array of T
Run Code Online (Sandbox Code Playgroud)
编辑:等等,我想我可能会得到你想要的东西.
T *(*a[N])[M]; // a is an N-element array of pointer to M-element
// array of pointer to T
Run Code Online (Sandbox Code Playgroud)
编辑:现在我们真的很傻:
T *(*(*a)[N])[M]; // a is a pointer to an N-element array of
// pointer to M-element array of pointer to T
T *(*(*(*f)())[N])[M]; // f is a pointer to a function returning
// a pointer to an N-element array of pointer
// to M-element array of pointer to T
T *(*(*f[N])())[M]; // f is an N-element array of pointer to
// function returning pointer to M-element
// array of pointer to T
Run Code Online (Sandbox Code Playgroud)
而对于病理上的疯狂:
T *(*(*(*(*(*f)())[N])())[M])(); // f is a pointer to a function
// returning a pointer to a N-element
// array of pointer to function
// returning M-element array of
// pointer to function returning
// pointer to T
Run Code Online (Sandbox Code Playgroud)
Typedef是为了wusses.
void* array[m][n];
Run Code Online (Sandbox Code Playgroud)
将为您提供一个虚拟指针的2D数组.除非我误解了你的问题,否则我不确定这有什么令人困惑的.