ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to initialize an array of structures, each with an integer and 4 integer pointers. I have written the following:
main(){
struct lattice_site{
int spin;
int *right_pointer;
int *left_pointer;
int *up_pointer;
int *down_pointer;
};
struct lattice_site mc_array[2];
mc_array[0] = {1,0,0,0,0};
return 0;
}
The compiler gives me the following error:
structure_tester.c: In function `main':
structure_tester.c:15: parse error before '{' token
line 15 refers to the line " mc_array[0] = {1,0,0,0,0}; "
What confuses me is that this syntax works when you aren't using an array of pointers. That is, if I replace mc_array[i] with mc_array in both places, it initializes just fine.
I really need to make a 2-dimensional array of structures like the one above. Will this work?
That way, all initialization stuff is in one place. The 'dummy' method is probably easier, and may even be a bit quicker, since it has no function calls.
main(){
struct lattice_site{
int spin;
int *right_pointer;
int *left_pointer;
int *up_pointer;
int *down_pointer;
};
struct lattice_site mc_array[2]={{1,0,0,0,0}};
// mc_array[0] = {1,0,0,0,0};
return 0;
}
This was easy to loop, though I had an extra dimension to deal with later on, and a lot of relationships between the structures and the pointers to keep track of. In fact, I'm still trying to get the details just right. Still, it's all working ok for a first attempt at a data structure!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.