Yeah thank you so much guys, seriously
Although i found out a new way to do what i wanted i really learned a lot and it enriched my knowledge of C, T H A N K - Y O U ))))
PS: will attempt to explain why!
Okay so, in this example what i want to do is i have two arrays of a structure and i want to use them with pointers, here's what i do first:
Code:
#define SHMKEY (key_t)0x10 //you need this one for shared memory
#define SHMKEY2 (key_t)0x9 // 0x9 because you want to take a hand on a diferent segment of memory (i think! haven't had time to test it more, but it works )
#define DIM_COUNTRY 4 // this is to define a size for the array of structures
then we have the structures and they're pointers:
Code:
struct Person {
int id;
char name[10];
};
struct Country {
struct Person person[DIM_COUNTRY];
};
struct Turma *country;
struct Turma *town;
Okay so, now we need the shared memory variables!
Code:
int shmid;
int shmidd;
shmid = shmget(SHMKEY, sizeof(struct Country), IPC_CREAT | 0777);
shmidd = shmget(SHMKEY2, sizeof(struct Country), IPC_CREAT | 0777);
Don't worry too much about the last parameter IPC_CREAT | 0777 i have no idea what it is.
Follow the leader on 2nd parameter, sizeof(struct <your_struct_array>) !
Why two shmid? Because as you can see right after them, the first parameter is what we defined right in the begining, one is SHMKEY points to memory 0x10 and the other is SHMKEY2 points to 0x9, otherwise both of your arrays of structures would point to the same memory segment and even though you would think you had diferent information because you had two diferent arrays you dont, they're pointing at the same location.
Imagine having 2 arrays, but they are part of one big array, if you had only ---> shmid = shmget(SHMKEY, sizeof(struct Turma), IPC_CREAT | 0777);
what would happen would be, both the information in your 2 arrays would be exactly the same, because they're source would be equal.
Moving on!
How do you access, and how do you put information into them? Quite easy! Except for the char part, thats why i included it!
Code:
strcpy(country->person[0].name, town->person[0].name);
(country->person[0]).id=(town->person[0]).id;
(turma->person[0]).id=0;
strcpy(town->person[0].name, "Filipe");
Okay so, first thing. To store information all you need to do is this:
country -> person [0] . id = 1;
pointer array position variable int value
Now to store a string! Because this is C you can't do it like in java and simply = "string" you must do this:
Code:
strcpy(country->person[0].name, "Filipe");
// strcpy(designated_name_for_variable_of_class.atribute, "write!");
So, because my pointer is country i have to put it first because it is what i want to change,
country->person[0] because its my array of persons in the position 0
country->person[0].name because its the name of my person in the position 0 of my array country that i want to change!
so going back a bit...
Code:
strcpy(country->person[0].name, town->person[0].name);
(country->person[0]).id=(town->person[0]).id;
(turma->person[0]).id=0;
strcpy(town->person[0].name, "Filipe");
So, to copy information from one string to the other, you just have to do what i did there!
Note the first line, i copy to country->person[0].name the string that is within town -> person[0].name
In the second i equal (because its a int) my country->person[0].id to my town->person[0].id
so my country->person[0].id has the value of town->person[0].id !
In the next two following lines of code it is how you can change just one position of the array singularly without copying from one array to the next!
I hope you understood it all, and i hope that it helps you programming better! ( this is mostly to the new people who might read the topic, given you two might know what i just wrote :P )
Thanks !