Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Technically, this isn't valid C. (It is valid C++, though.)</p> <pre><code>char *array[10]={}; </code></pre> <p>You should use</p> <pre><code>char *array[10] = {0}; </code></pre> <p>This declares an array of 10 pointers to char and initializes them all to null pointers.</p> <pre><code>char* token; token = "testing"; </code></pre> <p>This declares token as a pointer to char and points it at a string literal which is non-modifiable.</p> <pre><code>array[0] = "again"; </code></pre> <p>This points the first <code>char</code> pointer of <code>array</code> at a string literal which (again) is a non-modifiable sequence of char.</p> <pre><code>strcat(array[0], token); </code></pre> <p><code>strcat</code> concatenates one string onto the end of another string. For it to work the first string must be contained in writeable storage and have enough excess storage to contain the second string at and beyond the first terminating null character ('\0') in the first string. Neither of these hold for <code>array[0]</code> which is pointing directly at the string literal.</p> <p>What you need to do is something like this. (You need to <code>#include</code> <code>&lt;string.h&gt;</code> and <code>&lt;stdlib.h&gt;</code>.)</p> <p>I've gone for runtime calculation of sizes and dynamic allocation of memory as I'm assuming that you are doing a test for where the strings may not be of known size in the future. With the strings known at compile time you can avoid some (or most) of the work at compile time; but then you may as well do <code>"againtesting"</code> as a single string literal.</p> <pre><code>char* token = "testing"; char* other_token = "again"; /* Include extra space for string terminator */ size_t required_length = strlen(token) + strlen(other_token) + 1; /* Dynamically allocated a big enough buffer */ array[0] = malloc( required_length ); strcpy( array[0], other_token ); strcat( array[0], token ); /* More code... */ /* Free allocated buffer */ free( array[0] ); </code></pre>
 

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