title: how to use shared memory to communicate between two processes
body: <p>I am trying to communicate between two processes. I am trying to save data(like name, phone number, address) to shared memory in one process and trying to print that data through other process.</p>
  
  <p>process1.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/stat.h&gt;
  int main ()
  {
    int segment_id;
    char* shared_memory[3];
    int segment_size;
    key_t shm_key;
    int i=0;
    const int shared_segment_size = 0x6400;
    /* Allocate a shared memory segment. */
    segment_id = shmget (shm_key, shared_segment_size,
              IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
    /* Attach the shared memory segment. */
    shared_memory[3] = (char*) shmat (segment_id, 0, 0);
    printf ("shared memory attached at address %p\n", shared_memory);
    /* Write a string to the shared memory segment. */
     sprintf(shared_memory[i], "maddy \n");
     sprintf(shared_memory[i+1], "73453916\n");
     sprintf(shared_memory[i+2], "america\n");
  
    /*calling the other process*/
    system("./process2");
  
    /* Detach the shared memory segment. */
    shmdt (shared_memory);
    /* Deallocate the shared memory segment.*/
    shmctl (segment_id, IPC_RMID, 0);
  
    return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>process2.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/stat.h&gt;
  int main ()
  {
    int segment_id;
    char* shared_memory[3];
    int segment_size;
    int i=0;
    key_t shm_key;
    const int shared_segment_size = 0x6400;
    /* Allocate a shared memory segment. */
    segment_id = shmget (shm_key, shared_segment_size,
                S_IRUSR | S_IWUSR);
    /* Attach the shared memory segment. */
    shared_memory[3] = (char*) shmat (segment_id, 0, 0);
    printf ("shared memory22 attached at address %p\n", shared_memory);
     printf ("name=%s\n", shared_memory[i]);
     printf ("%s\n", shared_memory[i+1]);
     printf ("%s\n", shared_memory[i+2]);
    /* Detach the shared memory segment. */
    shmdt (shared_memory);
     return 0;
  }
  </code></pre>
  
  <p>But I am not getting the desired output.
  the output which i got is:</p>
  
  <pre><code>shared memory attached at address 0x7fff0fd2d460
  Segmentation fault
  </code></pre>
  
  <p>Anyone can please help me with this. Is this the correct way of initializing <code>shared_memory[3]</code>.</p>
  
  <p>Thank you.</p>