I have a struct that looks like this:
struct data {
int code;
char name[25], fnum[8];
};
Then I try to use it as a shared memory like this:
int dataSID = shmget(100002, sizeof(struct data), IPC_CREAT | 0666);
and attach it like this:
struct data *com = shmat(dataSID, 0, 0);
And finally I try filling the values likes this:
(*com).code = 1;
scanf("%s", (*com).name);
And I get segmentation fault on the scanf.
What am I doing wrong?
Does shmat properly allocate memory for the struct?
for character arrays you can always use strcpy() or strncpy(). Of which strncpy() is recommended which helps to avoid buffer overflow.