Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have several errors in your source code:</p> <ul> <li><p><code>struct *struct_array;</code> (l. 5)<br> What does it mean? Did you want to write <code>struct data *struct_array</code>? </p></li> <li><p><code>printf("%s ", struct_array[i].inputA);</code> (l.32 &amp; l. 33)<br> The argument <code>struct_array</code> masks the global declaration, and it is not an array. Why did you add this argument?</p></li> <li><p><code>struct_array = (int *)realloc(n * sizeof(int));</code> (l. 39)<br> You have forgotten an argument. Did you want to use <code>malloc</code> instead? Besides, the cast is not necessary (and incorrect!).</p></li> <li><p>Unless you are using an hosted environnment and C99/C11, you should return a value from <code>main</code>.</p></li> <li><p>Your variable <code>index</code> is not used. Why did you declare it?</p></li> <li><p><code>for(i = n; i &lt; n; i++)</code> (l. 53) You won't have any iteration here...</p></li> </ul> <p>The following code works as expected.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; /* TODO: Avoid global variables. */ struct data *struct_array; struct data { char inputA[20]; char inputB[20]; }; /* * TODO: Try to avoid passing your structure (40 bytes + padding) * without pointer. */ struct data get_data(void) { struct data thisdata; printf("Please enter input A\n"); /* TODO: Avoid using `scanf` for human inputs. */ scanf("%s", thisdata.inputA); printf("Please enter input B\n"); scanf("%s", thisdata.inputB); return thisdata; } void Output(size_t n) { size_t i; for (i = 0; i &lt; n; i++) { printf("%s ", struct_array[i].inputA); printf("%s ", struct_array[i].inputB); } } void resizeArray(size_t n) { /* TODO: Handle reallocations errors. */ struct_array = realloc(struct_array, n * sizeof *struct_array); } void mainMenu(void) { size_t i, n; int p; /* TODO: Use a loop ? */ printf("Please select from the following options:\n"); printf("1: Add new students to database\n"); printf("2: Display current student database contents\n"); printf("3: exit the program\n"); scanf("%d", &amp;p); switch (p) { case 1: printf("Please enter the number of students to register:\n"); scanf("%u", &amp;n); resizeArray(n); for (i = 0; i &lt; n; i++) struct_array[i] = get_data(); break; case 2: Output(n); break; } } int main(void) { struct_array = malloc(2 * sizeof(int)); mainMenu(); free(struct_array); return 0; } </code></pre>
    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. 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.
 

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