Assume: struct foo_t { int X,Y,Z; }
. Some function take an array of struct foo_t
and set some values to it; something like this:
void foo(struct foo_t *f, size_t limit, size_t *result_length)
{
int i = 0;
struct foo_t a;
a.X = 5;
//...
struct foo_t b;
b.X = 10;
// ...
struct foo_t c;
c.X = 4;
//...
f[i++] = a;
f[i++] = b;
f[i++] = c;
*result_length = i;
}
and then:
struct foo_t buf[12];
struct foo_t positive[12];
struct foo_t negative[12];
size_t len;
foo(buf, sizeof(buf)/sizeof(buf[0]), &len);
int c,positive_len,negative_len;
for(c = positive_len = negative_len = 0; c < len; c++)
{
if(buf[c].X < 8)
positive[positive_len++] = buf[c];
else
negative[negative_len++] = buf[c];
}
And finally:
puts("POSITIVE:");
int i;
for(i = 0; i < positive_len; i++)
printf("%d\n", positive[i].X);
puts("NEGATIVE:");
for(i = 0; i < negative_len; i++)
printf("%d\n", nagative[i].X);
The problem is the following: instead of getting "POSITIVE:\n4\n5"
, "NEGATIVE:10"
I'm getting 5 and 5
and 10
isn't printed. In other words, only the last value set. Why is this happening? I've reduced significantly my code to try to get some help here because the real function is around 300 lines of code that includes database management, etc; If really needed I will post here. Before to use = operator, I'd used memcpy()
to do copy of struct to my positive/negative arrays.