Note that there are some explanatory texts on larger screens.

plurals
  1. POSolve a segmentation fault in a pointer to a pointer to a string
    text
    copied!<p>I'm having a segmentation fault when I want to save a string in a dynamic array. <br> <br> I have a program that does this:<br> <br> User insert char "s"<br> The program enters a loop and save strings in an array (name: cod).<br> When user inserts char "t", it stops<br> <br> After that I save that array in the first position of a new dynamic array (name: vec).<br> <br> Then if user insert char "s" again<br> The program enters a loop and save strings in an array.<br> When user inserts char "t", it stops<br> <br> After that I save that array in the second position of a new dynamic array.<br> <br> and so one.<br> <br> <br></p> <p>This is my code:<br></p> <p>int main(){</p> <pre><code>char Cod[30][11]; char tmp[11]; char ***vec; int i = 0; strcpy (tmp, "p"); vec = (char *** ) malloc (sizeof ( char *) ); vec[0] = (char ** ) malloc (sizeof ( char *) * 30); do { scanf("%s", tmp); while( (strcmp (tmp, "p")) != 0){ strcpy ( Cod[i] , tmp ); scanf("%s", tmp); i++; } vec = (char ***) realloc (vec, sizeof ( char *) * (i + 1)); vec[i + 1] = (char ** ) realloc (vec[i + 1], sizeof ( char *) * (30)); vec[i-1] = (char **) Cod; scanf("%s", tmp); } while((strcmp (tmp, "s")) == 0); printf("%s", vec[0][0]); </code></pre> <p>return 0;</p> <p>}</p> <p>This is the part of the code that work's:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; int main(){ char Cod[30][11]; char tmp[11]; int i = 0; strcpy (tmp, "p"); do { scanf("%s", tmp); while( (strcmp (tmp, "p")) != 0){ strcpy ( Cod[i] , tmp ); scanf("%s", tmp); i++; } scanf("%s", tmp); } while((strcmp (tmp, "s")) == 0); printf("%s", Cod[0]); return 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