简体   繁体   中英

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] .

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] . The former is a pointer to pointer to char. The latter is a pointer to a block of 512 characters.

定义如下的功能。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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