0

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.

4

4 回答 4

1

您的代码中有错字:

   struct foo_t c;
   b.X = 4; // this should be c.X = 4;
   //...
于 2012-11-30T04:07:46.487 回答
1

您在上面的示例中有两个错别字/错误:

  1. 你没有设置 c

    结构 foo_t c;

    bX = 4;

  2. 此 printf 中的变量拼写错误

    for(i = 0; i <negative_len; i++)

    printf("%d\n", nagative[i].X);

于 2012-11-30T04:09:51.670 回答
1

有几个错误。有些是拼写,并且从未在您的“foo”函数中分配“c”。

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

typedef struct foo_t
{
    int X, Y, Z;
}foo_t;

void foo(struct foo_t *f, size_t limit, size_t *result_length)
{
   int i = 0;
   struct foo_t a, b, c;
   a.X = 5;
   //...
   b.X = 10;
   // ...
   c.X = 4; // CHANGE HERE FROM "B" to "C".
   //...
   f[i++] = a;
   f[i++] = b; 
   f[i++] = c;
   *result_length = i;
}

int main(int argc, char** argv)
{

    // CORRECTED ALL SPELLING ERRORS!!! (POSITIVE / NEGATIVE)
    struct foo_t buf[12];
    struct foo_t positive[12];
    struct foo_t negative[12];
    size_t len;
    int c, positive_len, negative_len;

    foo(buf, sizeof(buf)/sizeof(buf[0]), &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];
    }

    { // <-- IGNORE THIS BADNESS
        int i;
        puts("POSITIVE:");

        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", negative[i].X);
    }

    getchar();
}
于 2012-11-30T04:14:05.170 回答
1

这是从您的代码生成的 SSCCE(简短、独立、完整的示例):

#include <stdio.h>

struct foo_t { int X; };

static void foo(struct foo_t *f, size_t limit, size_t *result_length)
{
   size_t i = 0;
   struct foo_t a;
   a.X = 5;
   struct foo_t b;
   b.X = 10;
   struct foo_t c;
   c.X = 4;
   if (i < limit)
       f[i++] = a;
   if (i < limit)
       f[i++] = b; 
   if (i < limit)
       f[i++] = c;
   *result_length = i;
}

int main(void)
{
    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);
    size_t 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];
    }

    puts("POSITIVE:");
    for (size_t i = 0; i < positive_len; i++)
        printf("%d\n", positive[i].X);
    puts("NEGATIVE:");
    for (size_t i = 0; i < negative_len; i++)
        printf("%d\n", negative[i].X);
}

它产生:

POSITIVE:
5
4
NEGATIVE:
10

我不得不修复nagativetonegativepostiveto positive。我初始化了c.X. 我曾经limit确保没有溢出(并修复警告)。我更改了各种int计数器变量以size_t避免有关有符号与无符号比较的警告。我从结构中删除了 Y 和 Z 成员,因为在这个最小示例中没有使用它们。

于 2012-11-30T04:14:15.530 回答