Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, "obviously" you need to get "struct Database" filled in first:</p> <pre><code>struct Database MyDatabase; MyDatabase.data_rows=malloc(sizeof(MyDatabase.data_rows[0])*NumberOfPeople); </code></pre> <p>Ignoring the fact that I didn't check the malloc() for failure, this will give you an array of "struct Person", all uninitialized. So, most likely, you'll want to initialize them:</p> <pre><code>int i; for (i=0; i&lt;NumberOfPeople; i++) { struct Person* MyPerson; MyPerson=&amp;MyDatabase.data_rows[i]; MyPerson-&gt;id=i; MyPerson-&gt;name=malloc(...); /* Do something to store the name in MyPerson-&gt;name */ MyPerson-&gt;place=malloc(...); /* Do something to store the place in MyPerson-&gt;name */ } </code></pre> <p>Now, the problem here is the "..." I put on the malloc. It's easy if you use a fixed size, but then you could have just declared your struct to be something like</p> <pre><code>struct Person { int id; char name[100]; char place[200]; }; </code></pre> <p>Basically, I just can't tell what the length of the names should be, hence I just typed it as "...".</p> <p>Also, I just guessed what the "id" might be. Using the array index is actually somewhat pointless :-)</p> <p>Of course, you don't have to do it all now. You could just set the name and place pointers to NULL and fill them in later, like when you're reading the data from a file, or whatever you're planning to do. Or you could just not initialize it here at all, if you're confident that your code always "knows" which fields are initialized and which ones are not.</p>
    singulars
    1. This table or related slice is empty.
    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