title: C - Reading an integer through shared memory with mmap
body: <p>Currently I have got my shared memory working between 2 processes
  my parent looks like this</p>
  
  <pre><code>/* strings written to shared memory */
  const char *message_0 = "Hello";
  const char *message_1 = "World!";
  /* 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_RDWR, 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);
  /* write to the shared memory object */
  sprintf(ptr,"%s",message 0);
  ptr += strlen(message_0);
  sprintf(ptr,"%s",message 1);
  ptr += strlen(message_1);
  </code></pre>
  
  <p>and my child process receives the code like so</p>
  
  <pre><code>const char *name = "OS";
  /* shared memory file descriptor */
  int shm_fd;
  /* pointer to shared memory obect */
  void *ptr;
      /* open the shared memory object */
      shm_fd = shm_open(name,O_CREAT | O_RDWR, 0666);
      /* memory map the shared memory object */
      ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
      /* read from the shared memory object */
      //char message = ptr;
      //int newmsg;
      //newmsg = atoi(message);
      printf("%s",(char *)ptr);
      printf("\n");
  </code></pre>
  
  <p>Now instead of passing hello world I would like to pass arrays instead, so I tried changing the end of my parent to try passing a single integer.</p>
  
  <pre><code>sprintf(ptr, "%d", 5);
  ptr += 20; //just used 20 since it should be big enough for now
  </code></pre>
  
  <p>and in my child process I changed</p>
  
  <pre><code>printf("%d",(char *)ptr);
  </code></pre>
  
  <p>to   </p>
  
  <pre><code>printf("%s", (int *)ptr);
  </code></pre>
  
  <p>However my message always messes up somewhere, and I print an invalid number instead. Can anyone tell me what i am missing?</p>