Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How this works: <code>char *array[10]</code> is an array of 10 <code>char *</code> pointers (basically 10 same things as <code>token</code>).</p> <p><code>token = "testing"</code> creates static space somewhere in your program's memory at build time, and puts "testing" there. Then <em>in run time</em>, it puts address to that static "testing" to <code>token</code>.</p> <p><code>array[0] = "again"</code> does basically the same thing.</p> <p>Then, <code>strcat(array[0], token)</code> takes address in <code>array[0]</code>, and tries to add <code>token</code>'s content to string at that address. Which gives you segfault, since <code>array[0]</code> points to read-only data segment in your memory.</p> <p>How to do this properly:</p> <pre><code>char * initial = "first"; // pointer to static "first" string char * second = "another"; // another one char string[20]; // local array of 20 bytes strcpy(string, initial); // copies first string into your read-write memory strcat(string, second); // adds the second string there </code></pre> <p>Actually, if you don't want to shoot yourself in the foot, the better way to do something like the last two lines is:</p> <pre><code>snprintf(string, sizeof(string), "%s%s", initial, second); </code></pre> <p><code>snprintf</code> then makes sure that you don't use more than 20 bytes of <code>string</code>. <code>strcat</code> and <code>strcpy</code> would happily go over the limit into invalid memory, and cause another run-time segfault or something worse (think security exploits) if the copied string were longer then the destination space.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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