title: dynamically expanding array of struct
body: <p>I am (trying to) write a server-side daemon in c, and it accepts connections from clients. I need a struct that keeps information on each open connection, so I have created an array of my defined struct, and I have it dynamically re-sizing with realloc.</p>
  
  <p>The problem I have is creating the struct within the array.  I keep getting this error:</p>
  
  <pre><code>test.c:41: error: conversion to non-scalar type requested
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>I spend most of my time in PHP, and am a noob with c. I realize that I am making some simple, beginner mistakes (in other words, feel free to make fun of me). If I am doing something stupid, please let me know.  I've put my quality time in with google, but have not figured it out.  I have reproduced the issue on smaller scale, as below:</p>
  
  <p>here is my test.h:</p>
  
  <pre><code>typedef struct test_ test;
  </code></pre>
  
  <p>and here is my test.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "test.h"
  
  //define the struct
  struct test_ {
      int id;
      char *ip;
      int user;
      char *str;
  };
  
  //yes, the list needs to be global
  test *test_list;
  
  
  //
  // add an item to the global list
  //
  int add(int id, char *ip, int size)
  {
      //
      // increment size
      if(id&gt;size) {
          size = id;
          //try to expand the list
          test *tmp = realloc(test_list,size);
          if(tmp) {
              //it worked; copy list back
              test_list = tmp;
          } else {
              //out of memory
              printf("could now expand list\n");
              exit(1);
          }
      }
      //
      // HERE IS THE TROUBLE CODE::
      test_list[id] = (struct test)malloc(sizeof(test)+(sizeof(int)*5)+strlen(ip)+1);
      test_list[id].id = id;
      test_list[id].ip = malloc(strlen(ip));
          strcpy(test_list[id].ip,ip);
      test_list[id].user = 0;
      test_list[id].str = NULL;
  }
  
  //
  // main
  //
  int main(void)
  {
      //initialize
      int size = 1;
      test_list = malloc(size*sizeof(test));
      //add 10 dummy items
      int i;
      for(i=0; i&lt;10; i++) {
          size = add(i, "sample-ip-addr", size);
      }
      //that's it!
      return 0;
  }
  </code></pre>