【问题标题】:How many levels of indirection on Multidimensional Arrays are there?多维数组有多少个间接级别?
【发布时间】:2014-03-01 23:54:28
【问题描述】:

我正在使用 Microsoft Visual Studio Express 2013,试图做一些事情......代码实际上可以运行,但仍然存在错误,代码为 C4047:'char *' differs in levels of indirection from 'char[24][50]'

是这样吗?

忽略警告,该程序按我预期的方式运行,没有任何问题。我只是想了解和了解背后发生的事情。 (stale) 警告表示我在函数中传递多维数组的行。这是该函数的参数行:

void mass_assigner(
    WORD * translations,
    char * labels,
    char * PermBannedKeys,
    char * TempBannedKeys,
    char * Cooldowns
)
{ ... }

这是我从main 中调用它的方式:

...
mass_assigner(
    translations,
    labels,
    PermBannedKeys,
    TempBannedKeys,
    Cooldowns
);
...

其中labelschar labels[24][50] = { ... };

真正的问题是什么?据我所知,多维数组不是数组数组(具有多级间接),而只是一个数组(具有单级间接)。

【问题讨论】:

  • labels 是一个char **/指向字符串的指针 => 指向指针的指针

标签: c string multidimensional-array multiple-indirection


【解决方案1】:

如果您将二维数组传递给函数:

int labels[NROWS][NCOLUMNS];
f(labels);

函数的声明必须匹配:

void f(int labels[][NCOLUMNS])
{ ... }

void f(int (*ap)[NCOLUMNS]) /* ap is a pointer to an array */
{ ... }

【讨论】:

  • 我想补充一点,f(int labels[NROWS][NCOLUMNS]) { ... } 显然也可以,我已经做到了,可能没有必要,但我还是做到了。非常感谢。如果我只是将 ** labels 写为函数的参数,就会发生不想要的事情。
猜你喜欢
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 2010-09-24
  • 2010-12-19
相关资源
最近更新 更多