Note that there are some explanatory texts on larger screens.

plurals
  1. POAllocation - pointer-to-pointer
    text
    copied!<p>I would like to ask for help with allocation .... I got this homework to school...I have to write program which will load one G matrix and second G matrix and will search second G matrix for number of presences of the first G matrix....But, when I try to run my program I got Segmentation fault message... Thanks in advance. Example how the program is supposed to work....</p> <p>...</p> <p>Enter number of lines of wanted g matrix: 3 Enter the wanted g matrix:<br> 121212<br> 212121<br> 121212<br> G matrix to be searched:<br> 12121212121212<br> 21212121212121<br> 12121212123212<br> 21212121212121<br> 12121212121212<br> G matrix found 8 times. </p> <p>...</p> <p>this is my code: </p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; char * get_line(void) // get line { char * string; if((string = (char *)malloc(100 * sizeof(char))) == NULL) { printf("Nedostatek pameti.\n"); // not enough memory exit(1); } int i = 0; while((string[i] = fgetc(stdin)) != '\n') { i++; if((i % 100) == 0) { if((string = (char *)realloc(string, 100 * ( i - 1 ) * sizeof(char))) == NULL) { printf("Nedostatek pameti.\n"); // not enough memory exit(1); } } } return ( string ); } char ** get_wanted_g_matrix(int pocetradek /*number of lines*/) // get wanted g matrix { char ** string; printf("Zadejte hledanou matici:\n"); int i = 0; if(( * string = (char ** )malloc(100 * sizeof(char *))) == NULL) { printf("Nedostatek pameti.\n"); // not enough memory exit(1); } while(i &lt;= (pocetradek - 1)) { string[i] = get_line(); if((i &gt; 1) &amp;&amp; (*string[i-1] != strlen(*string[i]))) { printf("Nespravny vstup.\n"); // not enough memory exit(1); } printf("%s", string[i]); i++; if((i % 100) == 0) { if((* string = (char **)realloc(* string, 100 * ( i - 1 ) * sizeof(char *))) == NULL) { printf("Nedostatek pameti.\n"); // not enough memory exit(1); } } } return (string); } int get_number_of_lines(void) // get number of lines { int number_of_lines; printf("Zadejte pocet radek hledane matice:\n"); // enter the number of lines of wanted g matrix if(scanf("%d", &amp;number_of_lines) != 1) { printf("Nespravny vstup.\n"); // Error exit(1); } return ( number_of_lines ); } char ** get_searched_g_matrix(void) // get wanted g matrix { char ** string; printf("Matice, ktera bude prohledana:\n"); // G matrix to be searched int i = 0; if(( * string = (char ** )malloc(100 * sizeof(char *))) == NULL) { printf("Nedostatek pameti.\n"); // not enough memory exit(1); } while(!feof(stdin)) { string[i] = get_line(); if((i &gt; 1) &amp;&amp; (*string[i-1] != strlen(*string[i]))) { printf("Nespravny vstup.\n"); // error exit(1); } printf("%s", string[i]); i++; if((i % 100) == 0) { if((* string = (char **)realloc(* string, 100 * ( i - 1 ) * sizeof(char *))) == NULL) { printf("Nedostatek pameti.\n"); // not enough memory exit(1); } } } if(feof(stdin)) { return string; } } int search( char ** string1, char ** string2 ) // search { int string1width = strlen(*string1[0]); int string2width = strlen(*string2[0]); int string2height = strlen(**string2); int number_of_lines = get_number_of_lines(); unsigned int g = 0, h = 0, i2, j2, l = 0, i = 0, j; while( i &lt;= (string2height - 2) ) { j = 0; while( j &lt;= string2width - 2 ) { g = 0; h = 0; if(string2[i][j] == string1[g][h]) { i2 = i; while((g &lt;= number_of_lines - 1) &amp;&amp; (i2 &lt;= string2height - 2)) { j2 = j; h = 1; while(((string2[i2][j2] == string1[g][h]) &amp;&amp; (j2 &lt;= string2height - 2)) &amp;&amp; (h &lt;= string1width - 2)) { j2++; h++; } if(h != string1width - 1) { break; } if(g == number_of_lines - 1) { l++; break; } i2++; g++; } } j++; } i++; } return ( l ); } int main(void) { char ** string1; char ** string2; int number_of_lines = get_number_of_lines(); string1 = get_wanted_g_matrix(number_of_lines); string2 = get_searched_g_matrix(); if(feof(stdin)) { printf("Matice nalezena %d krat.\n", search( ** string1, **string2 )); // G matrix found %d times. } 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