2

I am having trouble initializing an array of structures in C. Here is my structure :

typedef struct Voie {
    int num;
    int sem_num[3];
    int crois[3];
} Voie;

The two arrays will contain 0, 2 or 3 integers.

I have an array of 12 "Voie" :

Voie voies[12];

And I want each of these to be initialized with these parameters :

{1,{0,16,1},{4,7,8}}
{2,{2,3},{4,5}}
{3,{},{}}
{4,{4,17,5},{7,10,11}}
{5,{6,7},{7,8}}
{6,{},{}}
{7,{8,17,9},{10,1,2}}
{8,{10,11},{10,11}}
{9,{},{}}
{10,{12,16,13},{1,4,5}}
{11,{14,15},{1,2}}
{12,{},{}}

I have tried with a function returning a structure, separating each case with a switch, but got redefinition issues. Then I found what I assume to be the best solution but still can't make it run :

int cas[][] = { {1,{0,16,1},{4,7,8}},
                    {2,{2,3},{4,5}},
                    {3,{},{}},
                    {4,{4,17,5},{7,10,11}},
                    {5,{6,7},{7,8}},
                    {6,{},{}},
                    {7,{8,17,9},{10,1,2}},
                    {8,{10,11},{10,11}},
                    {9,{},{}},
                    {10,{12,16,13},{1,4,5}},
                    {11,{14,15},{1,2}},
                    {12,{},{}}  };

for (i=0 ; i<12 ; i++) {
    voies[i] = cas[i];
}

I'm not even sure that this is possible, since the following works :

Voie v = {1,{0,16,1},{4,7,8}};

But not the following :

int tab[] = {1,{0,16,1},{4,7,8}};
Voie v = tab;

Also : how can I access to each of the elements in my structure once it is initialized ?

Thank you for your help.

6
  • What led you to this: int cas[][]? Commented May 17, 2015 at 13:49
  • This was the solution given on another post. The problem was almost the same as mine. Commented May 17, 2015 at 13:54
  • So many duplicates I do not know which one to use for closing this. Commented May 17, 2015 at 13:57
  • So you might like to link this answer? Commented May 17, 2015 at 13:57
  • Sorry, I just understand what you meant. The answer I'm talking about didn't say int cas[][] but int cas[][9] but since I didn't know what the size was in my case, I let it empty. stackoverflow.com/a/22927133/4403802 Commented May 17, 2015 at 14:05

3 Answers 3

6

You can simply initialize your array with:

EDITED:

Voie voies[12] = { 
                {1,{0,16,1},{4,7,8}},
                {2,{2,3,},{4,5,}},
                {3,{0,},{0,}},
                {4,{4,17,5},{7,10,11}},
                {5,{6,7,},{7,8,}},
                {6,{0,},{0,}},
                {7,{8,17,9},{10,1,2}},
                {8,{10,11,},{10,11,}},
                {9,{0,},{0,}},
                {10,{12,16,13},{1,4,5}},
                {11,{14,15,},{1,2,}},
                {12,{0,},{0,}}  
};
Sign up to request clarification or add additional context in comments.

5 Comments

It might be worth mentioning that all members/elements not being initialised explicitly in this case get initialised to 0 implicitly.
It is also worth mentioning that this is not standard C. Empty {} are not allowed. (They are for C++)
Which makes this answer simply wrong. There are enough C compilers out there that will flat out reject this as a syntax error. (Even if it's wrong in the question as well, it should be mentioned.)
Thanks for editing, it looks good (to me, anyway) now. One thing that might be worth covering as well is what @alk hints at too: instead of {3,{0,},{0,}},, you can simply write {3}, or {3,},. But if you prefer it the way you have it now, that's fine too.
Basically I want to do something for each element of the arrays. So currently I'm using a for loop, from 0 to 2, but if the array is empty, I don't want to do anything, and if it only contains 2 elements, I want to do something only 2 times. So I'm initializing the arrays with 3 elements anyway, which leads to some {-1,-1,-1}and {1,2,-1} and I test the value in the for loop and do stuff only if it's different from -1. What is the best way to do this ?
2

You can access the elements like voies[5].sem_num[0].

If Im not wrong, the example value above would be 17.

2 Comments

In the original example, voies[5].sem_num[0] is zero. You probably ment voies[6].sem_num[1] which is 17.
Exactly, Marian. I was just trying to see if everyone was paying attention... :P (JK, my mistake! Thanks for pointing out it!) @Scentle5S, just dont do like me did, ok?
2

Neither your declaration cas[][] nor your initialization with empty {} is standard C.

You may have at most one empty [] in a declaration, and you'd have to put at least one 0 inside the {}.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.