我有这需要一个结构作为参数,像一个函数:局部结构和指针
add_new_structure(structure s);
然后将其存储内部 structure structure_list[200];
问题:
1.当我想要使用结构,我有一个功能,如
structure *getStructure(int id)
{
return &structure_list[id];
}
是要去工作,如果我加一个结构是这样的:
void init()
{
structure test;
memset(&test,0,sizeof(structure));
add_new_structure(test);
}
,然后从另一个函数调用getStructure?像这样:
void anotherFunction()
{
structure *got_test = getStructure(0);
}
因为我记得我不能有局部变量,然后从另一个函数调用它吗?
2.是否更好地存储它像这样?
更改add_new_structure() parameter to structure *s
;
然后通过调用add_new_structure(& test)将其存储在 structure *structure_list[200];
中;
3.哪一个更好?或者什么是正确的做法?
'structure_list'是否有静态存储时间?你可能想要发布'add_new_structure()'和'struct'的定义。如果'struct'有任何指针,那么仅仅复制它们可能是危险的。 – hmjd
是的,它有一长串的结构成员,包含字符指针,所以如何安全地将它们存储在数组中?什么是静态存储时间? – steave
静态存储持续时间意味着该程序的生命周期持续(请参阅http://stackoverflow.com/questions/95890/what-is-a-variables-linkage-and-storage-specifier)。 – hmjd