...不使用 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
如果我们讨论的是指向二维数组的指针,那么事情只会变得稍微有趣一点:
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
编辑:等等,我想我可能得到了你想要的。
T *(*a[N])[M]; // a is an N-element array of pointer to M-element
// array of pointer to T
编辑:现在我们真的很傻:
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
对于病态的精神错乱者:
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
Typedef 是给胆小鬼用的。
关于c - 你将如何在 C 中声明一个二维指针数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1136146/