Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confusing param type of getopt

Tags:

c

getopt

typing

So, man page says getopt() takes char * const argv[], which is an array of constant pointers to char, as far as I understand. At the same time, getopt() permutes argv, so that eventually all non-options are at the end of the array.

I find it very confusing, because it now has to swap strings character by character instead of just swapping pointers, or so. Why doesn't it take just char * argv[]?

like image 928
leschtsch Avatar asked May 24 '26 12:05

leschtsch


1 Answers

From the man page for Linux:

Conforming To
POSIX.2 and POSIX.1-2001, provided the environment variable POSIXLY_CORRECT is set. Otherwise, the elements of argv aren't really const, because we permute them. We pretend they're const in the prototype to be compatible with other systems.

So you can omit the const qualifier on Linux.

like image 196
Barmar Avatar answered May 26 '26 09:05

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!