...不使用typedef。
我的老板声称他曾在面试中被问到这个问题,当他回答时,面试官告诉他不能使用typedef,因为这是一种糟糕的风格。
不管怎样,他喜欢向人们提出这个问题,只是想看看他们能否得到正确的答案,通常在新程序员午餐时进行。没有人能得到正确答案 (尤其是没有笔和纸或电脑方便的情况下)。我希望下次他再尝试用这个问题考验别人时,我能够做好准备>:D
...不使用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
类型定义是给懦夫的。
void* array[m][n];
这将给你一个二维的void指针数组。除非我误解了你的问题,否则我不确定为什么会有什么困惑。
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建议的那样;)
T *x = malloc(sizeof *x * num_elements);
在C89之前,malloc() 返回的是 char* 类型,因此需要进行强制转换,但现在很少有人使用20年以上的旧实现。 - John BodeC/C++中是行主序,Matlab/Fortran中是列主序,而对于某些未知原因的C#/本地交互多维数组,则不确定。
int x = 4;
int y = 4;
void** data = malloc(x * y * sizeof(void*));