Passing an array of strings as parameter to a function in C -


I need a simple task that gets a string and gives some array of stars after some parsing. So, this is my function signature:

  int parse (const char * foo, char ** sep_foo, int * sep_foo_qty) {int i; Four * tokens; ... strakpy (sep_fu [ii], token); / * SF here * / ...}  

Then I call it like this:

  Four sep_fu [MAX_QTY] [MAX_STRING_LENGTH] ; Four fu [MAX_STRING_LENGTH]; Int sep_foo_qty, error; ... error = pars (foo, sep_foo, and sep_foo_qyt); ...  

This way I get a warning during compiling:

  Warning: pass arguments 2 to 'parse' with incompatible index type  

And then / * sf * * /

What is wrong with a split mistake during my execution in the line marked in my C code?

Thank you in advance

The alert is absolutely fine. Your function wants an array of pointers to give you an array of arrays.

Expected:

 sep_foo: + ------ + + ----- + | Char ** | -> 0: | Four * | -> "String 1" + ------ + + ----- + 1: | Four * | -> "String 2" + ----- + * sep_foo_qty- 1: | ... | + ----- + 

What you have provided:

 sep_foo: + -------------------- - ---------- + 0: | Four [MAX_STRING_LENGTH] | + -------------------------------- + 1: | Four [MAX_STRING_LENGTH] | + -------------------------------- + MAX_QTY-1: | ... | + -------------------------------- + 

an array with elements of type X can be "decay" in a pointer-to- X , or X * . But changing the conversion to X is not allowed only one decay operation is allowed. You will need to be twice in your case, X array-key- MAX_STRING_LENGTH - is the car. The function wants that the X is pointer-to-four. Since these are not equal, the compiler warns you. I am a little surprised that this was just a warning because allowing the compiler to do so did nothing good.

In your function, you can write this code:

  four * y = faucet; * Sep_foo = y; This is the legal code since   

sep_foo a char ** , so * sep_foo a four * , and such is y ; You can assign them but whatever you tried to do, * sep_foo will not be such actually will not be char * ; This code will point to the four arrays, will try to do this in effect:

  Four destinations [MAX_STRING_LENGTH]; Char * y = NULL; Destination = y;  

You can not specify a pointer in an array, and therefore the compiler warns that the call is not good.

There are two ways to solve this:

  • The way you declare and assign sep_foo on the calling side, It matches the expectation of getting the function:

      four ** sep_foo = colok (MAX_QTY, size (four *)); For (Inti = 0; I  

    Or, equal to

      char * sep_foo [MAX_QTY]; For (Inti = 0; I  
  • Change the prototype of the function to accept what you are actually giving it to:

      int parse (Const char * foo, four seep_fu [MAX_QTY] [MAX_STRING_LENGTH], int * sep_fu_qti);  

Comments