Home > Back-end >  How to declare a variable as char* const*?
How to declare a variable as char* const*?

Time:10-09

I have a bluez header file get_opt.h where argv is an argument:

 extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,.....

which requires char* const* for argv. Because the unit is called from a different file I was trying to emulate argv but whatever permutation of declaring char, const and * is used I get unqualified-id or the compiler moans about not converting to char* const*.  I can get const char * easily, of course.  char const * argv = "0";  compiles OK in Netbeans with C 14 but char * const * produces "error: cannot convert 'const char *' to 'char * const *'" in initialisation (sic). How do you declare char* const* or am I mixing C with C so I'm up the wrong tree?

CodePudding user response:

Let's review the pointer declarations:

char *             -- mutable pointer to mutable data.  
char const *       -- mutable pointer to constant data.  
char       * const -- constant pointer to mutable data.  
char const * const -- constant pointer to constant data.

Read pointer declarations from right to left.

Looking at the descriptions, which kind of pointer do you want?

CodePudding user response:

Because void main(void) is in a different file I was trying to emulate argv

void main(void) is not the correct main function declaration. The main function has to have int return value.

Simply change the void main(void) to int main(int argc, char **argv) in that "other" file.

It does not require char const * to be passed to. Do not change the type of the main function arguments.

int getopt_long (int ___argc, char * const *___argv);

int main(int argc, char **argv) 
{
    printf("%d\n", getopt_long(argc, argv));
}

Both language compile fine: https://godbolt.org/z/5To5T931j

  • Related