gpt4 book ai didi

c - 在 C 中存储命令行参数的最佳方法

转载 作者:行者123 更新时间:2023-12-04 05:09:54 26 4
gpt4 key购买 nike

我倾向于使用 getopt()getopt_long在使用基于控制台的程序时解析命令行参数。然而,大多数时候我最终使用全局变量来存储我可以在所有 *.c 中使用的配置参数。文件。

所以我很想知道你使用什么模式,以及什么是全局变量的更好替代方案。

最佳答案

我一般定义一个结构:

struct ConfigurationOpts {
int interval;
int fullscreen;
/* ... */
};

并传递一个指向 struct ConfigurationOpts 实例的指针到其他模块:
int main() {
struct ConfigurationOpts conf;

/* ... */

init_submodule1(&conf);
init_submodule2(&conf);

return 0;
}

关于c - 在 C 中存储命令行参数的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15016512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com