title: Initializing an array of structs into shared memory
body: <p>I'm making 4 programs that creates a POSIX shared memory object, an array of structs, that will be shared by the other 3 processes.  Basically this project simulates files.  </p>
  
  <p>Program #1 creates the object.  Program #2 takes a filename and a string as arguments, then the filename and string (file contents) are saved to shared memory as a struct that is put in an available element of the array.  Program #3 will list the filenames.  Program #4 will search for a given file and display its contents.</p>
  
  <p>The trouble I'm having is initialize an array of structs into shared memory.  I keep getting the following errors, which tells me I'm using an incorrect method initializing the pointers:</p>
  
  <p><code>myformat.c:36: warning: initialization from incompatible pointer type</code></p>
  
  <p>I've searched this subject and found a few similar problems, but nothing really relative to my issue.  </p>
  
  <p>So, how do you properly initializing an array of structs into shared memory?</p>
  
  <p>Based on my research, I have coded the following.  Thanks!</p>
  
  <p>PROGRAM #1 (myformat.c):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/stat.h&gt;
  
  struct MyFiles
  {    
      char *fileName;    
      char *fileContents;    
  };
  
  int main()    
  {    
      /* the size of shared memory object */    
      int size = sizeof(struct MyFiles)* 20;
  
      /* name of the shared memory object */    
      const char *name = "/PROJ4_SHARED_MEM";     
  
      /* shared memory file descriptor */    
      int shm_fd;
  
      /* pointer to shared memory obect */    
      void *ptr;
  
      /* create the shared memory object */    
      shm_fd = shm_open(name, O_CREAT | O_RDRW, 0666);    
  
      /* configure the size of the shared memory object */    
      ftruncate(shm_fd, size);    
  
      /* memory map the shared memory object */    
      ptr = mmap(0, size, PROT_WRITE, MAP_SHARED, shm_fd, 0);    
      struct MyFiles* file = (struct MyStruct*)ptr;           
  
      /* save struct array to the shared memory object. Initialize first element. */    
      file[0]-&gt;fileName = "\0";    
      file[0]-&gt;fileContents = "\0";       
  
      return 0;
  
  }
  </code></pre>
  
  <p>PROGRAM #2 (mycreate.c):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/stat.h&gt;
  
  struct MyFiles
  {
      char *fileName;
      char *fileContents;
  };
  
  int main()
  {
      char *file_name = argv(0);
      char *file_contents = argv(1);
  
      /* the size of shared memory object */
      int size = sizeof(struct MyFiles)* 20;
  
      /* name  of  the  shared  memory  object */
      const  char  *name = "/PROJ4_SHARED_MEM";
  
      /* shared  memory  file descriptor */
      int shm_fd;
  
      /* pointer to  shared  memory  object  */
      void  *ptr;
  
      /* open the  shared  memory  object */
      shm_fd = shm_open(name, O_RDRW, 0666);
  
      /* memory map the shared memory object */
      ptr = mmap(0, size, PROT_WRITE, MAP_SHARED, shm_fd, 0);
      struct MyFiles* file = (struct MyStruct*)ptr;
  
      /*write to first available array slot in shared  memory  object.  Initialize next. */
      for (int i = 0; i &lt; 20; i++)
      {
          if (file[i].fileName == "\0")
          {
              sprintf(file[i]-&gt;fileName,"%s",file_name);
              sprintf(file[i]-&gt;fileContents,"%s",file_contents);
              file[i + 1]-&gt;fileName = "\0";
              file[i + 1]-&gt;fileContents = "\0";
              break;
          }
          else if (i == 19)
          {
              prinf("ERROR: The Shared Memory Object is full.\n\n");
              shm unlink(name);
              exit(1);
          }
      }
  
      /* remove the  shared  memory  object */
      shm unlink(name);
  
      return 0;
  }
  </code></pre>
  
  <p>PROGRAM #3 (myls.c):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/stat.h&gt;
  
  struct MyFiles
  {
      char *fileName;
      char *fileContents;
  };
  
  int main()
  {
      char *file_name = argv(0);
      char *file_contents = argv(1);
  
      int counter = 0;
  
      /* the size of shared memory object */
      int size = sizeof(struct MyFiles)* 20;
  
      /* name  of  the  shared  memory  object */
      const  char  *name = "/PROJ4_SHARED_MEM";
  
      /* shared  memory  file descriptor */
      int shm_fd;
  
      /* pointer to  shared  memory  object  */
      void  *ptr;
  
      /* open the  shared  memory  object */
      shm_fd = shm_open(name, O_RDONLY, 0666);
  
      /* memory map the shared memory object */
      ptr = mmap(0, size, PROT_READ, MAP_SHARED, shm_fd, 0);
      struct MyFiles* file = (struct MyStruct*)ptr;
  
      if (file[0].fileName == "\0")
      {
              prinf("ERROR: There are no saved files in the shared memory object.\n\n");
              exit(1);
      }
  
      /*List all filenames */
      while (file[counter].fileName != "\0";)
      {
          prinf("%s \n", file[counter]-&gt;fileName);
          counter++;
      }
  
      /* remove the  shared  memory  object */
      shm unlink(name);
  
      return 0;
  }
  </code></pre>