could you please help me understand this.
I have function that needs “char ***argv“;
As far as I understand it’s :
pointer to pointer to array of char pointers.
something like this: “char *arr[]” ?
char xx1 = '1';
char xx2 = '2';
char *argv[] = {&xx1,&xx2};
Then I call my function with gtk_init (&argc, &argv);
And get error:
main.cpp:43:31: error: cannot convert ‘char* (*)[2]’ to ‘char***’ for argument ‘2’ to ‘void gtk_init(int*, char***)’
Thank you for any help.
A
char***is a “pointer to pointer to pointer to char”. No arrays involved at all. Yourargvis an “array of 2 pointer to char”. The name of your array,argv, will decay to a pointer to its first element in certain circumstances – this pointer will have typechar**, or “pointer to pointer to char”.When you do
&argv, you get achar* (*)[2], or a “pointer to array of 2 pointer to char”. Which is not what you want. That’s because you’re taking the address of an array, not of a pointer.Also, you’re going to have a problem with the fact that you’re pointers in
argvare just pointing at singlechars, not at null-terminated strings.gtk_initwill almost certainly be expecting null-terminated strings.So what you can you do? Try this:
The reason for using arrays for the strings is because we need the
chars to be non-const, but achar* str = "hello";style declaration is deprecated – it must beconst char*. However, by using arrays, the contents of the string literal are copied into our array, so we can freely make it non-const.gtk_initreally just expects you to pass theargcandargvparameters of your main function to it like so:You may now ask “But why is
&argvnow allowed?argvis the same type as in the question! Why don’t we get a pointer to array again?” Actually,argvis not the same type, despite how much it looks like it is. When you define a function that takes an array argument, it actually gets transformed to a pointer. So the definition ofmainis equivalent to:So when we do
&argv, we’re totally fine, because it gives us achar***.As @OmnipotentEntity says in the comments – you’d better have a good excuse for not passing the parameters of
maintogtk_init.