Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Do like this: </p> <pre><code>char** list; list = malloc(sizeof(char*)*number_of_row); for(i=0;i&lt;number_of_row; i++) list[i] = malloc(sizeof(char)*number_of_col); </code></pre> <p>Additionally, if you are allocating memory dynamically. you are to free it as work done: </p> <pre><code>for(i=0;i&lt;number_of_row; i++) free(list[i] ); free(list); </code></pre> <p><strong>EDIT</strong> </p> <p>In you revised question: </p> <pre><code> int wordLine = 0, counter = 0, i; </code></pre> <p><code>wordLine</code> and <code>counter</code> are <code>0</code> </p> <p>before this code: </p> <pre><code>list = malloc(sizeof(char*)*wordLine+1); for(i = 0;i &lt; wordLine ; i++) list[i] = malloc(sizeof(char)*counter); </code></pre> <p>you have to assign value to <code>wordLine</code> and <code>counter</code> variable </p> <p>Also memory allocation should be before the following loop(outside): </p> <pre><code> while((c = fgetc(myFile)) != EOF){ : : } </code></pre> <p><strong>EDIT</strong>: </p> <p>New your third version of question. You are reading file two times. So you need to <a href="http://beej.us/guide/bgc/output/html/multipage/fseek.html" rel="nofollow"><strong>fseek(), rewind()</strong></a> to first char before second loop starts. </p> <p>try with: </p> <pre><code>fseek(fp, 0, SEEK_SET); // same as rewind() rewind(fp); // same as fseek(fp, 0, SEEK_SET) </code></pre> <p><sub>also I have doubt in your logic to calculate <code>numberOfLines</code> and <code>maxNumberOfChars</code>. please check that also</sub></p> <p><strong>EDIT</strong> </p> <p>I think your calculation for <code>maxNumberOfChars = 0, numberOfLines = 0</code> is wrong try like this: </p> <pre><code>maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0; while((c = fgetc(myFile)) !=EOF){ if(c == '\n'){ numberOfLines++; if(maxNumberOfChars &lt; numberOfChars) maxNumberOfChars = numberOfChars; numberOfChars=0 } numberOfChars++; } </code></pre> <p><code>maxNumberOfChars</code> is max number of chars in a line. </p> <p>Also change code: </p> <pre><code>malloc(sizeof(char)*(maxNumberOfChars + 1)); </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