假设:struct foo_t { int X,Y,Z; }
。某些函数采用struct foo_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];
}
最后:
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);
问题如下:我没有收到"POSITIVE:\n4\n5"
,"NEGATIVE:10"
而是5 and 5
,而10
并未打印memcpy()
。换句话说,只有最后一个值集。为什么会这样?我已经大大减少了我的代码,试图在这里获得一些帮助,因为真正的功能是大约300行代码,包括数据库管理等;如果真的需要我会在这里发布。在使用=运算符之前,我使用{{1}}将struct复制到我的正/负数组。
答案 0 :(得分:1)
您的代码中有拼写错误:
struct foo_t c;
b.X = 4; // this should be c.X = 4;
//...
答案 1 :(得分:1)
上面的例子中有两个错别字/错误:
您没有设置c
struct foo_t c;
b.X = 4;
此printf中的变量拼写错误
for(i = 0; i&lt; negative_len; i ++)
printf(“%d \ n”,nagative [i] .X);
答案 2 :(得分:1)
有几个错误。有些是拼写,“c”从未在你的“foo”函数中分配。
#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();
}
答案 3 :(得分:1)
这是从您的代码生成的SSCCE(Short, Self-Contained, Complete Example):
#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
我必须将nagative
修改为negative
,将postive
修改为positive
。我初始化了c.X
。我使用limit
来确保没有溢出(并修复警告)。我将各种int
计数器变量更改为size_t
,以避免有关已签名与未签名比较的警告。我从结构中删除了Y和Z成员,因为它们没有用在这个最小的例子中。