代码之家  ›  专栏  ›  技术社区  ›  Alex SeedmanJ

如何在C中声明一个二维指针数组?

  •  4
  • Alex SeedmanJ  · 技术社区  · 16 年前

    …不使用typedef。

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

    不管怎样,他喜欢把这个问题扔给别人,只是想看看他们是否能把它弄好,通常是在新程序员的午餐会上。没有人能把它弄好(尤其是没有纸笔或电脑)。我想下次他想用它来折磨别人时我会做好准备的。

    5 回复  |  直到 16 年前
        1
  •  6
  •   Falaina    16 年前
    void* array[m][n];
    

    将为您提供一个空指针的二维数组。除非我误解了你的问题,否则我不确定这件事到底是怎么回事。

        2
  •  9
  •   John Bode    16 年前

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

    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用于Wuses。

        3
  •  2
  •   Goz    16 年前
    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…

    编辑:哈哈哈或阿瓦卡的建议;)

        4
  •  0
  •   Sam Harwell    16 年前

    行主要在C/C++中,列在Matlab/fortran中,主要是因为一些未知的原因。

    int x = 4;
    int y = 4;
    void** data = malloc(x * y * sizeof(void*));
    
        5
  •  0
  •   Tobias Wärre    16 年前

    typedef和函数指针的作用是减轻程序员的心理压力。

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