Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have an <a href="https://stackoverflow.com/questionhttps://stackoverflow.com/questions/671703/array-index-out-of-bound-in-cs/671703/array-index-out-of-bound-in-c">index-out-of bound</a> mistake that causes <a href="http://en.wikipedia.org/wiki/Undefined_behavior" rel="nofollow noreferrer">undefined behavior</a> at runtime: </p> <pre><code>myStudPtr = &amp;myStud[SIZE]; // ^^^^^ // wrong </code></pre> <p>According to declaration <code>struct students myStud[SIZE];</code>, Maximum index value can be <code>SIZE - 1</code>. Remember array index starts with <code>0</code>.</p> <p><strong>Edit</strong></p> <p>As I can understand, you have declared an array of struct, and wants to read <code>i</code> number of student's information from user using pointer to struct. But there are some more problems in your code e.g. in for loop you always access <em>same</em> struct element: </p> <pre><code>for(count = 1; count &lt;= i; count++) scanf("%s", &amp;(*myStudPtr).name); // ^ // points to same struct element in array </code></pre> <p>This mistake is present in every <code>scanf()</code> and <code>printf()</code> statements. </p> <p>Correct will be as follows: </p> <ol> <li><p>Initialize pointer to the address of first element in array: </p> <pre><code> myStudPtr = &amp;myStud[0]; // ^ first element at 0th index </code></pre></li> <li><p>Via pointer you can simply access struct's elements in any one of following two ways: </p> <p><strong>First</strong>: for example to scan <code>score</code> value of a student: </p> <pre><code>scanf("%d", &amp;myStudPtr[count].score); </code></pre> <p><strong>Note:</strong> <a href="http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm" rel="nofollow noreferrer">precedence</a> of <code>[]</code> 'array subscript operator' is higher then <code>.</code> 'member selection via object name operator' so you do not need <code>()</code> parenthesis. Also precedence of <code>.</code> operator is higher then <code>&amp;</code> ampersand operator so even you do not need any parenthesis to get address(for example <code>&amp;(myStudPtr[count].score)</code> not needed). </p> <p><strong>Second</strong>: to scan <code>score</code> value of a student using pointer and <code>-&gt;</code> operator: </p> <pre><code>scanf("%d", &amp;(myStudPtr + count)-&gt;score); </code></pre> <p><strong>Note</strong>: <code>+</code> plus operator has lower precedence so we need parenthesis to overwrite precedence. And precedence of <code>-&gt;</code> member selection via pointer operator higher then <code>&amp;</code> ampersand operator so here also you do not need any parenthesis like <code>&amp;((myStudPtr + count)-&gt;score)</code>. </p></li> </ol> <p>Important to notes: </p> <ol> <li><p>You should check entered value of <code>i</code> by user that must be less to <code>SIZE</code> (the size of strcut array) otherwise you may have undefined behavior in your code. </p></li> <li><p>To read string use safe <code>fgets()</code> function instead of scanf to avoid buffer overflow. Read: <a href="https://stackoverflow.com/questions/17294809/reading-a-line-using-scanf-not-good/17294869#17294869">"Reading a line using <code>scanf()</code> not good?"</a></p></li> </ol> <p><strong>One more side note:</strong> </p> <ol> <li><p>As you are new C programmer(I feel) you should read: <a href="http://www.cs.arizona.edu/~mccann/indent_c.html" rel="nofollow noreferrer">Indenting C Programs</a> its a quick tutorial to learn indentation practice. </p></li> <li><p>You should always keep space after <code>,</code> and <code>;</code> to make your code readable, and for the same reason an expression like <code>count&lt;=i</code> should be written as <code>count &lt;= i</code>. Compare your for loop: </p> <pre><code> for(count=1;count&lt;=i;count++) </code></pre> <p>And following that I would like to suggest you: </p> <pre><code> for(count = 1; count &lt;= i; count++){ // code after one tab } </code></pre></li> </ol> <p><strong>Improve code:</strong> </p> <p>I would also like to suggest you to improve if-else coding style, your if-else part of code can be written as follows:</p> <pre><code>score = myStudPtr-&gt;score; if(0 &lt;= score &amp;&amp; score &lt;= 100){ if(score &gt; 70) printf("A"); if(60 &lt;= score &amp;&amp; score &lt; 69) printf("B"); if(50 &lt;= score &amp;&amp; score &lt; 59) printf("C"); if(40 &lt;= score &amp;&amp; score &lt; 49) printf("D"); if(score &lt; 40) printf("FAIL"); } else printf("Error: Invalid Entry!"); printf("\n"); </code></pre> <ul> <li>I removed many <code>else</code> statement, instead used &amp;&amp;. </li> <li>Removed redundant <code>{..}</code> braces pairs. </li> <li>Used local <code>score</code> variable instead <code>myStudPtr-&gt;score</code> to keep code look simple. </li> <li>Remove <code>\n</code> from each <code>pritf</code> statement instead add a new <code>printf</code> at the end (not very important).</li> </ul> <p><strong>Last mistake:</strong> </p> <p>To print each students record you need a new loop within that you call <code>printf()</code> function and includes if-else logic to evaluate student grades. </p>
    singulars
    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.
    3. 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