0

I'd like to have an array of instances of TestStruct with a single member of type int. I'd like the output to be

1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6

but it is 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

I'm a little stumped, because I don't really get how this isn't working:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int num;
} TestStruct;

int main(void) {
    int i, j;
    TestStruct *test = malloc(sizeof(TestStruct) * 18);

    for (i = 1; i <= 3; i++) {
        for (j = 0; j < 6; j++) {
            test[i].num = i;
        }
    }
    for (i = 0; i < 18; i++) {
        printf("%d ", test[i].num);
    }

    return 0;
}

If I just call printf() inside a nested for-loop, it works fine:

#include <stdio.h>

int main(void) {
    int i, j;

    for (i = 1; i <= 6; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", i);
        }
    }
}

Why is this happening?

3
  • 2
    Hint: What does test[i] do? Commented Dec 1, 2015 at 20:24
  • 1
    Your printing loop that "works fine" has the indices swapped if you compare it to the desired output. Commented Dec 1, 2015 at 20:32
  • @MOehm Fixed. I did a lot of edits and compiling, and apparently I got them swapped. Thank you! :) Commented Dec 1, 2015 at 20:42

2 Answers 2

3

you need to write it like this:

for (i = 0; i < 6; i++) {
        for (j = 0; j < 3; j++) {
            test[i*3+j].num = i+1;
        }
  }
Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. Accepted answer. Thank you!
2

Change you code to this, You should be indexing i element not j

 for (i = 1; i <= 3; i++) {
        for (j = 0; j < 6; j++) {
            test[j + (3*i)].num = i;
        }
  }

The above code will print 111111222222333333

In order to achieve 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 your code should look like below

for (i = 1; i <= 6; i++) {
            for (j = 0; j < 3; j++) {
                test[j + (3*i)].num = i;
            }
 }

Update: You need to keep track of outer loop or index you are appending number to so it does not rewrite everytime outer loop is run. I did it like this test[i + (3*i)]

7 Comments

This will initialise six instead of three elements. You need a combination of indices or a third, independent index.
Nabik, 2nd one has same problem again. you only write from element 1..6 in array. Result will be 012345600000000000
LOL, code still has problem. result will be 'segmentation fault'. :D
@Nakib I'll have to fiddle with it for a bit tomorrow. I accepted Afshin's post as answer, but thank you too!
Nakib, please take some care when posting your answers. Ideally, test them before posting them. You update repeatedly, but the updates aren't much better. For example, j + (3*i) will have values of 18 through 20 in the last iteration of the outer loop. These indices are out of bounds.
|

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.