0

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

2
  • You should not pass modifiable string without passing size information. Either use const or supply size Commented Feb 20, 2014 at 11:55
  • @stefanbachert I know I am supplying the size just did not include it in my post. Commented Feb 20, 2014 at 11:56

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, thank you. Apparently the parentheses around *cppStringArray are important. I was trying that without the parentheses!
@ajay i was talking about "parentheses around *cppStringArray " i.e. void function (char (* cppStringArray)[512], size_t num_strings); and not void function (char *cppStringArray[512], size_t num_strings);
1

Define the function like below.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.