简体   繁体   English

如何正确传递String数组作为函数参数?

[英]How to correctly pass a String array as function argument?

I know this has been asked numerous times before. 我知道之前已经多次询问过这个问题。 However, I'm unable to get rid of a warning. 但是,我无法摆脱警告。

void function (char** cppStringArray);
int main(void) {
    char cStringArray[5][512]={"","","","",""}; //Array of 5 Strings (char arrays) of 512 characters
    function (cStringArray); //warning: incompatible pointer type
    return 0;
}

How do I get rid of the warning? 我如何摆脱警告? It works if I declare the Stringarray as char* cStringArray[5] . 如果我将Stringarray声明为char* cStringArray[5]

If your string array will remain with those sizes then the best way is to use 如果您的字符串数组将保留这些大小,那么最好的方法是使用

void function (char (* cppStringArray)[512], size_t num_strings);

pass as 传递给

function(cStringArray, sizeof(*cStringArray)/sizeof(cStringArray));

Problem is that char** is not equivalent to char (*)[512] . 问题是char**不等同于char (*)[512] The former is a pointer to pointer to char. 前者是指向char的指针。 The latter is a pointer to a block of 512 characters. 后者是指向512个字符的块的指针。

定义如下的功能。

void function (char cppStringArray[5][512]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM