Note that there are some explanatory texts on larger screens.

plurals
  1. POReinitializing array of structs in function
    primarykey
    data
    text
    <p>I am new to this site and sort of new to programming.</p> <p>I am trying to declare an array of structs and then be able to initialize it in a function call. And then be able to reinitialize it with different values in Main and also be able to call the function to load different values. I am getting some compile errors that I need help with.</p> <p>My code:</p> <pre><code>#include &lt;stdio.h&gt; #define MAX_NAMES 5 typedef struct name // Struct template { int int_value; char *string_value; }Name; void init_struct(struct name SETTINGS); // Function prototype int main() { int i; Name settings[MAX_NAMES]; for(i=0; i&lt;=MAX_NAMES; i++) // Initializes struct { settings[i] = {1, "Test"}; } printf("Before function call:\n-------------\n"); for (i=0; i &lt; MAX_NAMES; i++) // Prints struct printf("%i : int_value = %i, string_value = %s\n", i, settings[i].int_value, settings[i].string_value); init_struct(settings[MAX_NAMES]); // Calls function to reinitalize struct printf("\n\nAfter function call:\n-------------\n"); for (i=0; i &lt; MAX_NAMES; i++) // Prints struct printf("%i : int_value = %i, string_value = %s\n", i, settings[i].int_value, settings[i].string_value); return 0; } void init_struct(struct name SETTINGS) { int i; for(i=0; i&lt;=MAX_NAMES; i++) // Redefines struct { SETTINGS[i] = {{2, "Test2"}}; } } </code></pre> <p>Compile errors:</p> <blockquote> <p>In function 'main':<br> |20| error: expected expression before '{' token<br> In function 'init_struct':<br> |45| error: subscripted value is neither array nor pointer nor vector<br> |45| error: expected expression before '{' token </p> </blockquote>
    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.
 

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