6

...不使用 typedef。

我的老板声称他曾经在一次采访中被问到这个问题,当他给出答案时,面试官告诉他他不能使用 typedef,因为它的风格很差。

无论如何,他喜欢向人们提出问题,只是为了看看他们是否能做对,通常是在新程序员的午餐会上。没有人能做对(尤其是没有笔和纸或手边的电脑)。我希望下次他试图用它难倒某人时做好准备>:D

4

5 回答 5

11

指向什么的二维指针数组?

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

Typedefs 是为 wusses 准备的。

于 2009-07-16T15:24:11.090 回答
8
void* array[m][n];

会给你一个 void 指针的二维数组。除非我误解了您的问题,否则我不确定这有什么令人困惑的地方。

于 2009-07-16T08:25:26.213 回答
2
void*** p2DArray = (void***)malloc( numAxis1 * sizeof( void** ) );

int count = 0;
while( count < numAxis1 )
{
    p2DArray[count] = (void**)malloc( numAxis2 * sizeof( void* ) );
    count++;
}

你去吧。您现在可以通过 p2DArray[n][m] 访问数组并获取存储在那里的 void*。

不明白为什么你仍然需要使用 typedefs ......

编辑:哈哈哈或 avakar 的建议;)

于 2009-07-16T08:24:34.810 回答
0

C/C++ 中的行专业,Matlab/Fortran 中的列专业以及由于某些未知原因 C#/native 与多维数组的互操作。

int x = 4;
int y = 4;
void** data = malloc(x * y * sizeof(void*));
于 2009-07-16T08:27:35.693 回答
0

关于 typedef 和函数指针的事情是它减轻了程序员的精神压力。

我想问题来自那里,使用 void 指针可以解决这个问题,尽管你必须在使用 FP 时强制转换它以消除所有警告。

于 2009-07-16T08:28:27.983 回答