Search code examples
ctypinggetopt

confusing param type of getopt


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[]?


Solution

  • 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.