LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-26-2004, 03:42 PM   #1
dreamgoat
LQ Newbie
 
Registered: Aug 2004
Posts: 6

Rep: Reputation: 0
initializing arrays of structures in c


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?

Thanks!
 
Old 09-26-2004, 06:54 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can set a struct equal to another one so you can set up a "dummy" struct instance and then loop...
Code:
#include <stdio.h>

int main(void)
{
  struct lattice_site
  {
    int spin;
    int *right_pointer;
    int *left_pointer;
    int *up_pointer;
    int *down_pointer;
  };
  struct lattice_site dummy = {1,0,0,0,0};
  struct lattice_site mc_array[2];
  int i;

  for(i = 0;i < 2;++i)
    mc_array[i] = dummy;

  return 0;
}
This might be a gcc extension, but I don't think it is.
 
Old 09-26-2004, 07:00 PM   #3
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
You could also use a sort of "constructor function" that initializes the struct:

Code:
void init_lattice_site(struct lattice_site lat)
{
  lat.spin = 1;
  lat.right_pointer = NULL;
  lat.left_pointer = NULL;
  lat.up_pointer = NULL;
  lat.down_pointer = NULL;
};

int main()
{
  /* stuff */
  for(i = 0;i < 2;++i)
    init_lattice_site(mc_array[i]);
}
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.
 
Old 09-27-2004, 01:12 AM   #4
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
I tried the original code by initializing the individual elements of the struct, viz.

mc_array[0].spin = 0 ;
mc_array[0].right_pointer = (int *) 1 ;
.
.
.

The above style works without warnings (using the default compiler with Sun OS 5.9).

However, mc_arrary[0] = { 1, 0, ... } or mc_array[0] = {1, (int *)0, ... } does not work ?!

Just what I observed.
 
Old 09-27-2004, 01:36 AM   #5
grub
Member
 
Registered: Mar 2003
Location: Kurnool, India.
Distribution: Redhat 9
Posts: 67

Rep: Reputation: 15
Initialize like this
Code:
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;
}
 
Old 09-27-2004, 02:02 AM   #6
dreamgoat
LQ Newbie
 
Registered: Aug 2004
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks everyone. These are all terrific replies. If anyone's interested, I ended up going with something like this one:

mc_array[0].spin = 0 ;
mc_array[0].right_pointer = (int *) 1 ;

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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regarding structures eshwar_ind Programming 2 04-25-2005 09:18 AM
initializing structures within functions.. miguetoo Programming 5 03-02-2004 07:50 PM
Structures AMMullan Programming 6 02-18-2004 11:39 AM
Nested structures :S? alitrix Programming 11 11-15-2003 07:13 PM
C and arrayed structures.. miguetoo Programming 9 05-22-2003 06:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration