Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The call to <code>getLine</code> is passing the <code>char*</code> pointer by value. The assignment to <code>line</code> within that function does not cause the allocated pointer to be returned to the caller. The function declaration should maybe be:</p> <pre><code>int getLine(char** line, FILE* file) {... </code></pre> <p>And then assign the result to <code>*line</code>. And the call to the function would need to pass the address:</p> <pre><code>getLine( &amp;line, text ); </code></pre> <p>You could also use a local variable for the usage inside the function and then assign the final result to <code>*line</code> prior to returning. That might make the code a bit simpler to understand. Otherwise, each time <code>line</code> is used, it would be necessary to dereference the pointer and it gets (just my opinion here) a bit messier. So maybe change the parameter in the function definition to <code>getLine( char** retLine, ... )</code>. Then declare a local variable of the form <code>char* line;</code>. Then prior to the <code>return</code> statements, assign it:</p> <pre><code>*retLine = line; </code></pre> <p>A very incomplete example:</p> <pre><code>int getLine( char **retLine, FILE *file ) { *retLine = NULL; // make sure we don't return garbage if error occurs char *line = malloc( ... ); // do stuff with line, fill it up, etc. ... // assign the final result to the output param *retLine = line; return length; } </code></pre> <p>This is basically a preference issue. Otherwise, the dereferencing must be done. For example,</p> <pre><code>*line = malloc(...); int length = strcspn( *line, ... ); </code></pre> <p>etc.</p>
 

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