如何消除传递多维数组为const多维数组的警告? [英] How to eliminate warning for passing multidimensional array as const multidimensional array?

查看:113
本文介绍了如何消除传递多维数组为const多维数组的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出code:

/* signatures */
int getParams(char params[MAX_PARAM_LEN][MAX_LINE_LEN]);
int getVersion(const char params[MAX_PARAM_LEN][MAX_LINE_LEN],
               const char* tagName );
/* initializing */
char params[MAX_PARAM_LEN][MAX_LINE_LEN] = {};

/* getting parameters */
paramCount = getParams(params); /* OK, params match with getParams signature */

/* processing the params array */
i = getVersion(params, "version"); /* warning: passing arg 1 of `getVersion' from incompatible pointer type */

我看到常量性的问题,但我不知道为什么或如何避免它。我要的是它不能修改 PARAMS 了一个函数。任何意见是值得欢迎的(除了禁用此警告或处理功能删除常量)。

I see that the constness is the problem, but I don't know why or how to avoid it. What I want is a function which can't modify the params anymore. Any advice is welcome(besides disabling this warning or deleting const in the processing function).

感谢:
Visko

Thanks: Visko

推荐答案

您不能消除这些C警告,而不进行显式转换为正确的类型。如果没有一个typedef这会很难看,虽然

You can't eliminate these warnings in C without making an explicit cast to the proper type. Without a typedef this is going to look ugly though

i = getVersion((const char (*)[MAX_LINE_LEN]) params, "version")

这是特定于C语言很怪。在C ++中这个问题是固定的。

This is a strange quirk specific to C language. In C++ this issue was fixed.

顺便说一句,在 {} 初始化是你是如何获得在C非法的的编译?

BTW, the {} initializer is illegal in C. How did you manage to get that to compile?

这篇关于如何消除传递多维数组为const多维数组的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆