Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The warning is exactly right. Your function wants an array of pointers. You're giving it an array of arrays.</p> <p>Expected:</p> <pre> sep_foo: +------+ +-----+ |char**|--> 0: |char*|-->"string1" +------+ +-----+ 1: |char*|-->"string2" +-----+ *sep_foo_qty-1: |... | +-----+ </pre> <p>What you provided:</p> <pre> sep_foo: +--------------------------------+ 0: | char[MAX_STRING_LENGTH] | +--------------------------------+ 1: | char[MAX_STRING_LENGTH] | +--------------------------------+ MAX_QTY-1: | ... | +--------------------------------+ </pre> <p>An array with elements of type <code>X</code> can "decay" into a pointer-to-<code>X</code>, or <code>X*</code>. But the value of <code>X</code> isn't allowed to change in that conversion. Only <em>one</em> decay operation is allowed. You'd need it to happen twice. In your case, <code>X</code> is array-of-<code>MAX_STRING_LENGTH</code>-chars. The function wants <code>X</code> to be pointer-to-char. Since those aren't the same, the compiler warns you. I'm a bit surprised it was just a warning since nothing good can come from what the compiler allowed to happen.</p> <p>In your function, you could write this code:</p> <pre><code>char* y = NULL; *sep_foo = y; </code></pre> <p>That's legal code since <code>sep_foo</code> is a <code>char**</code>, so <code>*sep_foo</code> is a <code>char*</code>, and so is <code>y</code>; you can assign them. But with what you tried to do, <code>*sep_foo</code> wouldn't <em>really</em> be a <code>char*</code>; it would be pointing to an array of char. Your code, in effect, would be attempting to do this:</p> <pre><code>char destination[MAX_STRING_LENGTH]; char* y = NULL; destination = y; </code></pre> <p>You can't assign a pointer into an array, and so the compiler warns that the call is no good.</p> <p>There are two ways to solve this:</p> <ul> <li><p>Change the way you declare and allocate <code>sep_foo</code> on the calling side so it matches what the function expects to receive:</p> <pre><code>char** sep_foo = calloc(MAX_QTY, sizeof(char*)); for (int i = 0; i &lt; MAX_QTY; ++i) sep_foo[i] = malloc(MAX_STRING_LENGTH); </code></pre> <p>or, equivalently</p> <pre><code>char* sep_foo[MAX_QTY]; for (int i = 0; i &lt; MAX_QTY; ++i) sep_foo[i] = malloc(MAX_STRING_LENGTH); </code></pre></li> <li><p>Change the prototype of the function to accept what you're really giving it:</p> <pre><code>int parse(const char *foo, char sep_foo[MAX_QTY][MAX_STRING_LENGTH], int *sep_foo_qty); </code></pre></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload