Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, with this:</p> <pre><code>Name settings[MAX_NAMES]; for(i=0; i&lt;=MAX_NAMES; i++) // Initializes struct { settings[i] = {1, "Test"}; } </code></pre> <p>You can only use that kind of initializer list when you're <em>defining</em> a struct, which you already did with <code>Name settings[MAX_NAMES]</code>. You're going to have to do <code>settings[i].int_value = 1;</code> and so on.</p> <p>For your second problem, you declare a function that accepts a <code>struct name</code>, but you try to treat it as an array. You should change it to:</p> <pre><code>void init_struct(struct name * SETTINGS) { int i; for(i=0; i &lt; MAX_NAMES; i++) { SETTINGS[i].int_value = 2; SETTINGS[i].string_value = "Test2"; } } </code></pre> <p>Note the change from <code>i &lt;= MAX_NAMES</code> to <code>i &lt; MAX_NAMES</code>. You'll need to make this change in your first <code>for</code> loop, too.</p> <p>You'll also need to change <code>init_struct(settings[MAX_NAMES]);</code> to <code>init_struct(settings)</code> when you call that function.</p> <p>Like so:</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].int_value = 1; settings[i].string_value = "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); // 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].int_value = 2; SETTINGS[i].string_value = "Test2"; } } </code></pre> <p>Output:</p> <pre><code>paul@local:~/src/c/scratch$ ./stu Before function call: ------------- 0 : int_value = 1, string_value = Test 1 : int_value = 1, string_value = Test 2 : int_value = 1, string_value = Test 3 : int_value = 1, string_value = Test 4 : int_value = 1, string_value = Test After function call: ------------- 0 : int_value = 2, string_value = Test2 1 : int_value = 2, string_value = Test2 2 : int_value = 2, string_value = Test2 3 : int_value = 2, string_value = Test2 4 : int_value = 2, string_value = Test2 paul@local:~/src/c/scratch$ </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