...不使用typedef。
我的老板声称他曾经在接受采访时被问过,当他回答时,采访者告诉他他不能使用typedef因为风格很差。
无论如何,他喜欢向人们提出这个问题只是为了看看他们是否能够做到正确,通常是在新的程序员午餐时间。没有人能做对(特别是没有笔和纸或电脑)。我希望下次他试图用它来阻止某人>:D
时做好准备答案 0 :(得分:9)
2D数组指针到底是什么?
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
如果我们谈论的是指向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
编辑:等等,我想我可能会得到你想要的东西。
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是为了wusses。
答案 1 :(得分:6)
void* array[m][n];
将为您提供一个虚拟指针的2D数组。除非我误解了你的问题,否则我不确定这有什么令人困惑的事。
答案 2 :(得分: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 *。
不知道为什么你还需要使用typedef ......
编辑:hahahaha或avakar建议的内容;)答案 3 :(得分:0)
主要用于C / C ++,Matlab / Fortran专栏,以及一些未知原因C#/与多维数组的本地互操作。
int x = 4;
int y = 4;
void** data = malloc(x * y * sizeof(void*));
答案 4 :(得分:0)
关于typedef和函数指针的事情是它减轻了程序员的精神压力。
我猜问题来自那里,使用void指针可以解决这个问题,尽管你必须在每次使用它时抛出FP来消除所有警告。