const char * 和 strdup 在 C 中与 getopt 一起使用时是否具有相同的功能?

发布于 2024-07-08 07:35:56 字数 644 浏览 7 评论 0原文

在下面的代码片段中,我可以将 char * 替换为 const char * 并删除 strdup() 函数调用并直接获取 getopt() 设置的 optarg 值吗? 建议我使用 const char * 来跳过 strdup 函数的使用。 提前感谢您的帮助。

/* Code Snippet */
char *dir = NULL; 
char *bld = NULL;
int chr;

while ( ( chr = getopt( argc, argv, "d:a:b:f:" ) ) != -1 ) {

 switch ( chr ) {

  case 'd': //Directory parameter
   dir = strdup( optarg );
   if (dir == NULL) { /*Error*/ }

  case 'b': //Build parameter
   bld = strdup( optarg );
   if (bld == NULL) { /*Error*/ }
  ...other code...
 }
} 

我真的不明白这样做的必要性。

编辑:感谢您的回答。 这真的很有帮助。 我将代码修改为 const char * 并跳过了 strdup 调用。

谢谢, 丽菊

In the below code snippet can i replace char * to const char * and remove the strdup() function call and directly take the optarg value set by getopt()? I am advised to use const char * to skip the strdup function usage. Appreciate the help in advance.

/* Code Snippet */
char *dir = NULL; 
char *bld = NULL;
int chr;

while ( ( chr = getopt( argc, argv, "d:a:b:f:" ) ) != -1 ) {

 switch ( chr ) {

  case 'd': //Directory parameter
   dir = strdup( optarg );
   if (dir == NULL) { /*Error*/ }

  case 'b': //Build parameter
   bld = strdup( optarg );
   if (bld == NULL) { /*Error*/ }
  ...other code...
 }
} 

I really don't understand the need for doing so.

Edit: Thanks for the answers. It was really helpful. I modified the code to const char * and skipped the strdup call.

Thanks,
Liju

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木緿 2024-07-15 07:35:56

由于 optarg 被声明为“extern char *optarg;”,因此您不必使用“const char *”,但最好不要将参数修改为你的程序。 我建议,这将更多地取决于您使用 dirbld 变量调用的函数是否是常量正确的。 如果它们是您的代码,您可以使它们如此; 如果不是,您可能会发现保持变量为非 const 更简单。 您无需调用“strdup()”(这是 POSIX 中的标准,但在 C 中不是,如 ISO/IEC 9899:1999)。

Since optarg is declared as 'extern char *optarg;', you do not have to use 'const char *', but it is good practice not to modify the arguments to your program. It will depend more, I suggest, on whether the functions you call with your dir and bld variables are const-correct. If they are your code, you can make them so; if not, you may find it simpler to keep the variables non-const. Your call to 'strdup()' (which is standard in POSIX, but not in C - as in ISO/IEC 9899:1999) is unnecessary.

只怪假的太真实 2024-07-15 07:35:56

我的理解来自 (http://linux.die.net/man/3/optarg) 是 optarg 指向 argv 缓冲区的内容,因此不需要 strdup。 使用 const char* 的建议似乎是一个合理的想法。

My understanding from (http://linux.die.net/man/3/optarg) is that optarg points to the contents of the argv buffer so the strdup isn't necessary. The suggestion to use const char* seems a reasonable idea.

月亮邮递员 2024-07-15 07:35:56

两者都是很好的建议。 您可能不想更改 dir 和 bld,因此将它们声明为 const char * 而不是 char * 在任何情况下都是有用的,因为编译器会检测到更多编码错误。 通过摆脱 strdup,您只是避免了不必要的复制(取决于您之后对 dir 所做的操作),但它会为您的 optarg 创建一个别名。

Both are good pieces of advice. You probably don't want to change dir and bld, so declaring them as const char * instead of char * is useful in any case because the compiler will detect more coding errors. By getting rid of the strdup you are just avoiding an unnecessary copy (depending on what you do with dir afterwards), but it creates an alias to your optarg.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文