Structure inside Shared Memory [Segmentation Fault Problem]
Okay guys, first off I am new here, thanks for this forum, I guess this is a nice place to get some good answers to many problems :)
I will try to be clearly and fast, so I can get your help easly :)
So what I am trying to do is:
Store 30 structures inside a shared memory. Its like a app to store notes, 30 notes, like I said.
My problem is on the iteration between structures that are on shared memory.
My code:
Code:
struct note{ //Okay here is my structure for each note
int date;
char title[30];
char text[900];
char user[20];
}
struct nota mynote_structure; //here I declare a note structure
main(){
struct note *ptr; //here I declare a pointer to a structure, I will need this for the shared memory
int shmID; //here the ID for shmget
shmID = shmget(1000, sizeof(mynote_structure) * 30, 0600 | IPC_CREAT); // I put the size of structure * 30, so I have room for 30 structures (30 notes) in that shared memory
if(shmID < 0){
printf("Error creating shared memory");
exit(1);
}
ptr = shmat(shmID, NULL, 0); // now I attach the shared memory to a pointer of type note , that is the type of my notes structure.
if(ptr == NULL){
printf("Error attaching shared memory");
exit(1);
}
//and now i test if actually there are structures, reading the int date, that should return 0;
printf("Date is %d", (*ptr).date);
//this should return the date of the first structure on the shared memory, if I would like the second structure date, I would do (*ptr+1).date, I guess.
}
The problem is that I am getting segmentation fault on that last printf, when I search for the date.
Guys I am begginer on C, I would really appreciate some help on this, I cant get it on my mind, I study the pointers on arrays of chars, and this is how it works, I guess that it should work for structures too, that are on shared memory right?
Thanks alot in advance,
DarkLink