c - 最佳实践方法,在库中创建指向 typdef 的指针数组,并将其指针传递给其他源模块
问题描述
我正在创建一个库。需要从 init 函数返回指向 typdef 的顶级关键指针数组。做这个的最好方式是什么?
是否在 Init 函数之外创建数组,...并在其他源模块中将其声明为 extern?
myType *myArray[256];
void Init()
{
// seed the 1st element.
myArray[0] = malloc(sizeof(myType));
// initialize some variables in the 1st myType.
}
...还是我在 Init 中完成所有操作:
// library.c
void *Init()
{
static myType *myArray[256];
// seed the 1st element.
myArray[0] = malloc(sizeof(myType));
// initialize some variables in the 1st myType.
return myArray;
}
// main.c
extern myType *myArray[256];
int main ()
{
myArray = Init();
}
……或者完全是别的什么?
谢谢!
解决方案
推荐阅读
- java - 矩阵归一化
- html - 我可以使用 flexbox 将徽标放置在导航栏的中心吗?
- javafx - Javafx 使用堆栈窗格更改窗格
- c - 使用 arm-none-eabi 工具链时重新定义 fputc 函数
- angularjs - 无法从 AngularJS Scope 中获取价值
- java - 使用媒体播放器播放 .M3U 链接
- c# - 如何使用日期范围获取目录中的文件?
- nlog - Xml 配置中的 NLog default-target-parameters 不适用于以编程方式添加的文件目标
- python-3.x - Boto3 - get_change()、change_resource_record_set() INSYNC 需要 60 秒才能返回
- javascript - 使用函数访问类方法