【问题标题】:How would you declare a two dimensional array of pointers in C?你将如何在 C 中声明一个二维指针数组?
【发布时间】:2010-11-11 06:59:12
【问题描述】:

...不使用 typedef。

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

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

【问题讨论】:

  • 那么从什么时候开始 typedef 的风格不好?我想在明天之前在我的办公桌上看到负责这个想法的人的名字。
  • avakar 是 ... 完全正确,我不明白这个问题的意义。也许你的老板实际上用 C 编程已经 10 多年了,他隐约记得这很困难?还是您忘记包含“挑战”的某些部分?
  • @DrJokepu: typedefs 不能被前向声明。通常,这在 C 中并不是真正的问题,但在 C++ 中却是。这不利于模块化。
  • 我终于想通了;他要求一个指针数组,指向一个指向某事物的指针数组:T *(*a[N])[M]。有关主题的变体,请参阅下面的答案。
  • cdecl 是您解决此类问题的好朋友。

标签: c arrays multidimensional-array function-pointers


【解决方案1】:

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

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 是为 wusses 准备的。

【讨论】:

  • "Typedef 是为 wusses 设计的。" LOOOOOL
  • 纯粹为上述报价投票。
【解决方案2】:
void* array[m][n];

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

【讨论】:

    【解决方案3】:
    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 的建议;)

    【讨论】:

    • 在 C 中,你永远不应该强制返回 malloc()。
    • @unwind:你永远不应该使用 malloc 的返回值吗?所以你不能分配除了 void* 之外的任何东西,它本身是无用的?
    • Nah char* pChar = malloc( 1 );如果我没记错的话是有效的代码。我似乎不记得强制返回 malloc 有什么害处……但我确实使用 C++ 而不是 C。
    • 危害是如果你忘记包含 malloc() 的定义,强制转换会隐藏你的错误。因为它没有真正的目的......
    • void 指针被隐式转换为其他对象指针类型,而不需要强制转换。 T x = malloc(sizeof *x * num_elements);是惯用的 C。在 C89 malloc() 返回 char 之前,因此需要进行强制转换,但并不是很多人都在从事 20 多年前的实现。
    【解决方案4】:

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

    int x = 4;
    int y = 4;
    void** data = malloc(x * y * sizeof(void*));
    

    【讨论】:

      【解决方案5】:

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

      我猜问题出在那儿,使用 void 指针可以解决这个问题,尽管在使用 FP 时必须强制转换以消除所有警告。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-20
        • 1970-01-01
        • 2016-02-21
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 2010-12-21
        相关资源
        最近更新 更多