0

I am trying to wrap my head about the concept of 2-dimensional arrays (of structs) in C

Say I have the following definition:

typedef struct group group;

struct group {
    int members;
    int neighbours;
    char color;
};

#define NUM_CELLS 10

With the following function that is supposed to copy some data from a single array to a multidimensional array:

void test_mgroup_arr(group clusters[][NUM_CELLS],group tests[NUM_CELLS], int num_groups) {

    int i;
    int j = 0;

    for (i = 0; i < num_groups; ++i)
        clusters[i][j] = tests[i];
}

This is called like:

int num_groups = 5;
group clusters[NUM_CELLS][NUM_CELLS];

group tests[NUM_CELLS];
tests[0].members = 101;
tests[0].neighbours = 111;

tests[1].members = 102;
tests[1].neighbours = 112;

tests[2].members = 103;
tests[2].neighbours = 113;

tests[3].members = 104;
tests[3].neighbours = 114;

tests[4] = tests[3];

test_mgroup_arr(clusters, tests, num_groups);

I expect the code in the function to copy the 5 items from the test array to the right place in the multi-dimensional array. However, this does not work as expected, and even segfaults in some cases.

How is this not correct? What would be the right way of copying a struct from a 1dim array to a 2 dim array?

4
  • I can't see a problem (in relation to the seg fault) with this. Is there any other code you have omitted? Commented Sep 27, 2012 at 11:26
  • @hmjd The complete code that segfaults (on the clusters[i][j] = test[i] line) gist.github.com/3793595 Commented Sep 27, 2012 at 11:45
  • I expect the code in the function to copy the 5 items from the test array to the right place in the multi-dimensional array. define "right place". If you want test[n] == clusters[n][0], then you're doing it right. If you want test[n] == clusters[0][n] then your test_mgroup_arr() is backwards Commented Sep 27, 2012 at 12:12
  • Thanks for all the help, I seem to have messed up some of my tests... It seems the segfault is later... Commented Sep 27, 2012 at 12:13

2 Answers 2

1

I do not see an issue in how you are passing the array or accessing it. The code actually looks right, and gives me the right results. Note:

for(i = 0; i < num_groups, ++i)
   clusters[i][j] = tests[i];

Lets say the address of:

clusters[0][0] is 0x0047F7EC
clusters[1][0] is 0x0047F864
clusters[2][0] is 0x0047F8DC

sizeof(group) = 0xC * 0xA (num of groups) = 0x78

So you can see the math works out here. j always == 0, so what's i doing? The address of clusters+i is:

0x0047F7EC for i=0
0x0047F864 for i=1
0x0047F8DC for i=2

Exactly what you'd expect. When I get out of test_mygroup_arr I get values of clusters[0][0], clusters[1][0], clusters[2][0], clusters[3][0], clusters[4][0] are set to the values in tests[0], tests[1], tests[2], tests[3], tests[4] respectively.

That's what you were going for correct?

Did you try printing out the addresses to see what was happening? Is there more to your code that you're not showing? I'm wondering if something else is the cause of your seg fault. I assume you're using a C99 Compiler?

Note: My test of your code that worked fine was only and exactly what you posted.

int main()
{
   int num_groups = 5; 
   ...
   test_mgroup_arr(clusters, tests, num_groups); 
   return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Actually to pass array as parameter you have to pass pointer on it`s first element, this is how compiler expects it, so instead of

void test_mgroup_arr(group clusters[][NUM_CELLS],group tests[NUM_CELLS], int num_groups) use

void test_mgroup_arr(group (*)[NUM_CELLS],group tests[NUM_CELLS], int num_groups)

3 Comments

The pointer does not have a name now (clusters). I don't see how to put the name in between there.
Your syntax is wrong, please use OP's names: void test_mgroup_arr(group (*clusters)[NUM_CELLS], group tests[NUM_CELLS], int num_groups) Also this doesn't matter, both are equivalent ways of passing the array.
No they are not equivalents when passing multidimensional array (2 dimensional in this case)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.