我在使用以下代码段时遇到了一些问题:
#include <stdio.h>
struct some_numbers
{
int id;
char *somestring;
};
typedef struct some_numbers numb;
void print_numbers(numb *a)
{
printf("%d: %s\n", a->id, a->somestring);
}
void add_number(numb *a)
{
// do someting magical
// push the new result to the existing struct
// put something into like:
a->somestring[5] = "widdley";
}
int main(void)
{
// put some stuff in the struct
numb entries[50];
int x;
for(x=0; x < 4; x++)
{
numb a = entries[x];
a.id = x;
a.somestring = "goats";
print_numbers(&a);
}
add_numbers(&a); // i want to call a method
return 0;
}
我想创建一个结构数组,将结构传递给一个方法,并将更多项目弹出到数组中。到目前为止,我所尝试的一切都以惨败告终,我很难想出办法摆脱这个难题。我可以毫无问题地打印这些值:
> ./struct
0: goats
1: goats
2: goats
3: goats
>
我希望输出看起来像:
> ./struct
0: goats
1: goats
2: goats
3: goats
4: widdley
>
请帮忙。我不擅长c,所以要温柔!
编辑:澄清了代码示例以将焦点从错误的区域上移开。