Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 复制结构问题_C_Struct - Fatal编程技术网

C 复制结构问题

C 复制结构问题,c,struct,C,Struct,假设:struct foo_t{intx,Y,Z;}。一些函数获取一个struct foo\u t数组并为其设置一些值;大概是这样的: 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;

假设:
struct foo_t{intx,Y,Z;}
。一些函数获取一个
struct foo\u t
数组并为其设置一些值;大概是这样的:

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;
}
然后:

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];
}
struct foo_t buf[12];
结构foo_t阳性[12];
结构foo_ut负数[12];
尺寸透镜;
foo(buf,sizeof(buf)/sizeof(buf[0]),&len);
int c,正片,负片;
对于(c=正的\u len=负的\u len=0;c
最后:

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);
put(“正:”);
int i;
对于(i=0;i

问题是:我得到的不是
“正:\n4\n5”
“负:10”
而是
5和5,而
10
没有打印出来。换句话说,仅设置最后一个值。为什么会这样?为了在这里获得一些帮助,我大大减少了代码,因为真正的功能是大约300行代码,包括数据库管理等;如果真的需要,我会在这里发布。在使用=运算符之前,我使用
memcpy()
将struct复制到我的正/负数组中。

您的代码中有一个输入错误:

   struct foo_t c;
   b.X = 4; // this should be c.X = 4;
   //...
#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);
}

您的代码中有一个输入错误:

   struct foo_t c;
   b.X = 4; // this should be c.X = 4;
   //...
#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);
}

在上面的示例中,您有两个输入错误/错误:

  • 你没有设置c

    结构foo_t c

    b、 X=4

  • 此printf中的变量拼写错误

    对于(i=0;i printf(“%d\n”,否定[i].X)


  • 在上面的示例中,您有两个输入错误/错误:

  • 你没有设置c

    结构foo_t c

    b、 X=4

  • 此printf中的变量拼写错误

    对于(i=0;i printf(“%d\n”,否定[i].X)


  • 有几个错误。有些是拼写错误,在“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();
    }
    
    #包括
    #包括
    #包括
    typedef struct foo\u t
    {
    int X,Y,Z;
    }富特;
    void foo(结构foo\u t*f、大小限制、大小*结果长度)
    {
    int i=0;
    结构foo_t a、b、c;
    a、 X=5;
    //...
    b、 X=10;
    // ...
    c、 X=4;//此处从“B”改为“c”。
    //...
    f[i++]=a;
    f[i++]=b;
    f[i++]=c;
    *结果_长度=i;
    }
    int main(int argc,字符**argv)
    {
    //更正了所有拼写错误!!!(肯定/否定)
    结构foo_t buf[12];
    结构foo_t阳性[12];
    结构为负[12];
    尺寸透镜;
    int c,正片,负片;
    foo(buf,sizeof(buf)/sizeof(buf[0]),&len);
    对于(c=正的\u len=负的\u len=0;c{/有几个错误。有些是拼写错误,在“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();
    }
    
    #包括
    #包括
    #包括
    typedef struct foo\u t
    {
    int X,Y,Z;
    }富特;
    void foo(结构foo\u t*f、大小限制、大小*结果长度)
    {
    int i=0;
    结构foo_t a、b、c;
    a、 X=5;
    //...
    b、 X=10;
    // ...
    c、 X=4;//此处从“B”改为“c”。
    //...
    f[i++]=a;
    f[i++]=b;
    f[i++]=c;
    *结果_长度=i;
    }
    int main(int argc,字符**argv)
    {
    //更正了所有拼写错误!!!(肯定/否定)
    结构foo_t buf[12];
    结构foo_t阳性[12];
    结构为负[12];
    尺寸透镜;
    int c,正片,负片;
    foo(buf,sizeof(buf)/sizeof(buf[0]),&len);
    对于(c=正的\u len=负的\u len=0;c
    
       struct foo_t c;
       b.X = 4; // this should be c.X = 4;
       //...
    
    #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);
    }
    
    我必须将
    nagative
    修正为
    negative
    positive
    修正为
    positive
    。我初始化
    c.X
    。我使用
    限制来确保没有溢出(并修正警告)。我将各种
    int
    计数器变量更改为
    size\u t
    ,以避免有关有符号与无符号比较的警告。我从结构中删除了Y和Z成员,因为在这个最小示例中没有使用它们。

    这是从代码生成的SSCCE():

       struct foo_t c;
       b.X = 4; // this should be c.X = 4;
       //...
    
    #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);
    }
    

    我必须将
    nagative
    修正为
    negative
    positive
    修正为
    positive
    。我初始化
    c.X
    。我使用
    限制来确保没有溢出(并修正警告)。我将各种
    int
    计数器变量更改为
    size\u t
    ,以避免有关有符号与无符号比较的警告。我从结构中删除了Y和Z成员,因为在这个最小的示例中没有使用它们。

    以前使用
    memcpy
    时它工作吗?我不清楚您得到的输出。是
     肯定的:5\n否定的:5
    ?@philipvr:Not,我得到的是
    肯定的:5\n5\n
    而不是
    肯定的:4\n5\n
    你可以通过在进行调试时添加打印信息来进行调试…这很容易-与使用调试器逐步完成代码一样容易,或者比使用调试器更容易。到处打印值…int c,POSITIVE,negative;…POSITIVE是错误的led(多个位置),所以是“负”在好几个地方。你以前使用
    memcpy
    时它能工作吗?我不清楚你得到的输出。它是
    正片:5\n负片:5
    ?@philipvr:not,我得到的是
    正片:5\n5\n
    而不是
    正片:4\n5\n
    ,你可以在进行调试时添加打印信息…这很简单-和我们一样简单,或者比我们更简单正在调试一个调试器以逐步完成代码。在所有地方打印值…int c,positive,negative;…positive拼写错误(几个地方),在几个地方“negative”拼写错误。谢谢。但这段代码只是一个示例;我在新线程的文本区域键入了它