Hi. I have a multidimensional arrays of char's that I'm using to hold strings. The multidimensional array is shared with a forked process. I'm getting a segmentation fault when I try to use strcpy to place a string into one of the rows of the mulitidimensional array. Here's my code.
Code:
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
int shmid;
int main()
{
int id, index;
char **shmaddr = (char **)malloc(7 * sizeof(char *));
shmaddr[0] = (char *)malloc(7 * 50 * sizeof(char));
for (index = 0; index < 7; index++)
{
shmaddr[index] = shmaddr[0] + index * 50;
}
shmid = shmget(IPC_PRIVATE, 4096, IPC_CREAT|0644);
shmaddr = shmat(shmid, (void *) 0, 0);
fork();
strcpy(shmaddr[0],"stuff");
printf("First position of shared memory has contents %s\n", shmaddr[0]);
if (id != 0){
shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0);}
return 0;
}
code tags fixed by Hammer.