Cpp pointers and arrays - c++

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. Your argv is 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 type char**, or "pointer to pointer to char".
When you do &argv, you get a char* (*)[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 argv are just pointing at single chars, not at null-terminated strings. gtk_init will almost certainly be expecting null-terminated strings.
So what you can you do? Try this:
char xx1[] = "1"; // Now these are null-terminated strings
char xx2[] = "2";
char* argv[] = {xx1, xx2};
char** argvp = argv; // Use the fact that the array will decay
gtk_init(&argc, &argv); // &argv will now be a char***
The reason for using arrays for the strings is because we need the chars to be non-const, but a char* str = "hello"; style declaration is deprecated - it must be const 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_init really just expects you to pass the argc and argv parameters of your main function to it like so:
int main(int argc, char* argv[])
{
gtk_init(&argc, &argv);
// ...
}
You may now ask "But why is &argv now allowed? argv is the same type as in the question! Why don't we get a pointer to array again?" Actually, argv is 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 of main is equivalent to:
int main(int argc, char** argv);
So when we do &argv, we're totally fine, because it gives us a char***.
As #OmnipotentEntity says in the comments - you'd better have a good excuse for not passing the parameters of main to gtk_init.

Related

How to declare a variable as char* const*?

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 * argv = "0";
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?
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?
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
Credit #JaMiT above
[I don't know how to accept a commment]
Also, you might want to notice how you can remove the call to getopt_long() from that example while retaining the error (it's motivation for your declaration, but not the source of your issue). If you think about your presentation enough, you might realize that your question is not how to declare a char* const* but how to initialize it. Due to arrays decaying to a pointer, you could declare an array of pointers (char* const argv[]) instead of a pointer to pointer (char* const * argv). See The 1D array of pointers way portion of A: 2D Pointer initialization for how to initialize such a beast. Beware: given your setup, the "2D array way" from that answer is not an option. Reminder: The last pointer in the argument vector is null; argv[argc] == nullptr when those values are provided by the system to main().

What is a char** argv[] in C++?

I am clear about the difference between char* argv[] and char** argv (as explained, for instance, in this question).
But what kind of type is char** argv[] ? A pointer to a pointer to a pointer of chars? And what would that be, when used to pass arguments to the program?
For the record, the type occurs in the following declaration of a C++ library I am trying to interface to Python:
/** Initialize the Stage library. Stage will parse the argument
array looking for parameters in the conventional way. */
void Init( int* argc, char** argv[] );
If main definition is main(int argc, char * argv[]) and you want to send argv via pointer, you will do Init( &argc, &argv ). And the corresponding parameter can be char ** argv[].
I assume that this Init function will make changes to the argc and argv that main has received, so it needs pointers to them.
If the library uses some program arguments, the Init function can handle them, and then remove those arguments from the argument list before it is processed by the rest of the program.
char** argv[] is an array of char**, IOW an array of pointers to pointers to chars, or more likely an array of pointers to null-terminated character strings, eg:
char *str1 = "...";
char *str2 = "....";
char** argv[2];
argv[0] = &str1;
argv[1] = &str2;
However, if argv is coming from main() itself, then char** is a typo, it must be char* instead:
char* argv[]
But if argv is coming from some library instead, it very well could be wrapping the original argv array from main() inside its own array, in which case char**[] might make sense so it can point at the original strings and not have to copy them.

Initialize char**

I'm very new to C++. I'm trying to call a function that takes in char**:
bool func(char** a) {
//blablabla
}
So it takes in an array of c-strings. I need to create a char**, but nothing works.
char** a = char[255][255]; // error: type name is not allowed
char** a = new char[255][255]; // error: a value of type "char (*)[255]" cannot be used to initialize an entity of type "char **"
char a[][] = {"banana", "apple"};
char** b = &a; // error: a value of type "<error-type> (*)[2]" cannot be used to initialize an entity of type "char **"
At the end I need to do:
char* a[] = {"banana", "apple"};
Why the first few didn't work and why the last one worked?
Thanks in advance.
There's a lot wrong in your code.
char** a = char[255][255]; // error: type name is not allowed
First of all this is not even valid C++ (or C for that matter). Maybe you meant:
char a[255][255];
In any case always remember that the type of a bi-dimensional dynamically allocated array is not ** but (*)[N] which is very different.
char** a = new char[255][255]; // error: a value of type "char (*)[255]" cannot be used to initialize an entity of type "char **"
The error message you provide in the comment explains exactly what I said earlier.
char a[][] = {"banana", "apple"};
In the above code the correct type of the variable a should be char* a[]. Again, arrays and pointer (for what the type is concerned) are very different things. A char array may decay to pointer (if NULL terminated), but for the rest, except with explicit casts, you can't use pointers and arrays like you are doing.
The last one worked because, like I said earlier, char* [] is the correct type for an array of C-strings.
Anyway, if you just doing homework, it is ok to learn this things. But in future development using C++: try not to use "features" that start with C-, like C-strings, C-arrays, etc. C++'s standard library gives you std::string, std::array, std::vector and such for free.
If you really need to allocate dynamic memory (with new and delete, or new[] and delete[]) please use smart pointers, like std::shared_ptr or std::unique_ptr.
You say you are working in C++. Then you can easily ignore const char* and char** and focus about what you can use:
#include <string>
#include <vector>
std::vector<std::string> arrayOfStrings;
arrayOfStrings.push_back("foo");
bool func(const std::vector<std::string>>& a) {
..
}
If you know the size at compile time you can even use std::array:
std::array<255, std::string> fixedArrayOfStrings
EDIT: since you need to build an array of C strings in any case you can easily do it starting from the vector:
const char **arrayOfCstrings = new const char*[vector.size()];
for (int i = 0; i < vector.size(); ++i)
arrayOfCstrings[i] = vector[i].c_str();
func(arrayOfCstrings);
delete [] arrayOfCstrings;
char**
is ambiguous - it can mean:
pointer to pointer
array of c-strings - experienced programmer would write char* arr[] instead
In the first case it is quite simple:
char* niceString = GetNiceString();
func(&niceString);
however in the second case it is slightly more complex. The function will not know the length of the array so you need to end it explicitly with a NULL, just like for example environ is:
char* a[3] = { "One", "Two", NULL }; /* note that this is possibly dangerous
because you assign const char* (READ-ONLY) to char* (WRITABLE) */
func(a); // char*[] gets downgraded to char** implicitly
so char** a = char[255][255]; it's weird to C and C++
and if you want a static 2d array just
char a[255][255];
char ** is a scalar type, you have to cast to (char *[]), try this :
char **temp = (char *[]){"abc", "def","fg"};

C++ - char** argv vs. char* argv[]

What is the difference between char** argv and char* argv[]? in int main(int argc, char** argv) and int main(int argc, char* argv[])?
Are they the same? Especially that the first part does not have [].
They are entirely equivalent. char *argv[] must be read as array of pointers to char and an array argument is demoted to a pointer, so pointer to pointer to char, or char **.
This is the same in C.
They are indeed exactly the same.
The golden rule of arrays to remember is:
"The name of an array is a pointer to the first element of the array."
So if you declare the following:
char text[] = "A string of characters.";
Then the variable "text" is a pointer to the first char in the array of chars you've just declared. In other words, "text" is of type char *. When you access an element of an array using [index], what you're actually doing is adding an offset of index to the pointer to the first element of the array, and then dereferencing this new pointer. The following two lines will therefore initialize both variables to 't':
char thirdChar = text[3];
char thirdChar2 = *(text+3);
Using the square brackets is a convience provided by the language that makes the code much more readable. But the way this works is very important when you start thinking about more complex things, such as pointers to pointers. char** argv is the same as char* argv[] because in the second case "the name of the array is a pointer to the first element in the array".
From this you should also be able to see why it is that array indexes start from 0. The pointer to the first element is the array's variable name (golden rule again) plus an offset of... nothing!
I've had debates with a friend of mine as to which is better to use here. With the char* argv[] notation it may be clearer to the reader that this is in fact an "array of pointers to characters" as opposed to the char** argv notation which can be read as a "pointer to a pointer to a character". My opinion is that this latter notation doesn't convey as much information to the reader.
It's good to know that they're exactly the same, but for readablity I think that if the intention is an array of pointers then the char* argv[] notation conveys this much more clearly.
For the first part of the question:
char** argv: pointer to a pointer to a char
char* argv[]: pointer to an array
So the question is whether a pointer to a type C and an array C[] are the same things. They are not at all in general, BUT they are equivalent when used in signatures.
In other words, there is no difference in your example, but it is important to keep in mind the difference between pointer and array otherwise.
For all practical purposes, they're the same. This is due to C/C++'s handling of arrays passed as arguments, where an array decays to a pointer.
The bracket form is only useful in statement declarations like:
char *a[] = {"foo", "bar", "baz"};
printf("%d\n", sizeof a / sizeof *a);
// prints 3
Because it knows at compile time the size of the array. When you pass a bracket form as parameter to a function (main or some other one), the compiler has no idea what the size of the array would be at runtime, so it is exactly the same as char **a. I prefer char **argv since it's clearer that sizeof wouldn't work like it would on the statement declaration form.
There is a difference between TYPE * NAME and TYPE NAME[] in both C and C++. In C++ both types are not interchagneable. For example following function is illegal (you will get an error) in C++, but legal in C (you will get warning):
int some (int *a[3]) // a is array of dimension 3 of pointers to int
{
return sizeof a;
}
int main ()
{
int x[3][3];
std::cout << some(x)<< std::endl;
return 0;
}
To make it legal just change signature to int some (int (*a)[3]) (pointer to array of 3 ints) or int some (int a[][3]). The number in last square brackets must be equal to an argument's. Converting from array of arrays to an array of pointers is illegal. Converting from pointer to pointer to array of arrays is illegal too. But converting pointer to pointer to an array of pointers is legal!
So remember: Only nearest to dereference type signature doesn't matter, others do (in the context of pointers and arrays, sure).
Consider we have a as pointer to pointer to int:
int ** a;
&a -> a -> *a -> **a
(1) (2) (3) (4)
You cannot change this value, the type is int ***. May be taken by function as int **b[] or int ***b. The best is int *** const b.
The type is int **. May be taken by function as int *b[] or int ** b. Brackets of the array declaratin may be leaved empty or contain any number.
The type is int *. May be taken by function as int b[] or int * b or even void * b
Should be taken as int parameter. I don't want to fall into details, like implicit constructor call.
Answering your question: the real type of argumets in main function is char ** argv, so it may be easily represented as char *argv[] (but not as char (*argv)[]). Also argv name of main function may be safely changed.
You may check it easily: std::cout << typeid(argv).name(); (PPc = pointer to p. to char)
By the way: there is a cool feature, passing arrays as references:
void somef(int (&arr)[3])
{
printf("%i", (sizeof arr)/(sizeof(int))); // will print 3!
}
Moreover pointer to anything may be implicitly accepted (converted) by function as void pointer. But only single pointer (not pointer to pointer etc.).
Further reading:
Bjarne Stroustrup, C++, chapter 7.4
C pointers FAQ
This is a simple example I came up with, which have two functions (Main_1, Main_2) take the same arguments as the main function.
I hope this clear things up..
#include <iostream>
void Main_1(int argc, char **argv)
{
for (int i = 0; i < argc; i++)
{
std::cout << *(argv + i) << std::endl;
}
}
void Main_2(int argc, char *argv[])
{
for (int i = 0; i < argc; i++)
{
std::cout << *(argv + i) << std::endl;
}
}
int main()
{
// character arrays with null terminators (0 or '\o')
char arg1[] = {'h', 'e', 'l', 'l', 'o', 0};
char arg2[] = {'h', 'o', 'w', 0};
char arg3[] = {'a', 'r', 'e', '\0'};
char arg4[] = {'y', 'o', 'u', '\n', '\0'};
// arguments count
int argc = 4;
// array of char pointers (point to each character array (arg1, arg2, arg3 and arg4)
char *argPtrs[] = {arg1, arg2, arg3, arg4};
// pointer to char pointer array (argPtrs)
char **argv = argPtrs;
Main_1(argc, argv);
Main_2(argc, argv);
// or
Main_1(argc, argPtrs);
Main_2(argc, argPtrs);
return 0;
}
Output :
hello
how
are
you
hello
how
are
you
hello
how
are
you
hello
how
are
you
Both are same for your usage except for the following subtle differences:
Sizeof will give different results for both
Also second one may not be reassigned to new memory area since it's an array
With second one you can use only those indexes which are
valid. It's unspecified by C/C++ if you try to use an array index beyond
array length. However with char** you can use any index from 0 to ...
Second form can only be used as formal parameters to a function. While first can even be used to declare variables within a stack.

How do I pass an array as a parameter?

This is driving me crazy:
I have function
void gst_init(int *argc, char **argv[]);
in the gstreamer api and I want to call it with some parameters I define, like:
int argc = 2;
char* argv[2] = {"myvalue1", "myvalue2"};
gst_init(&argc, &argv);
This code doesn't compile (I get error C2664):
error C2664: 'gst_init' : cannot convert parameter 2 from 'char *(*)[2]' to 'char **[]'
The question: How do I define the argv value to pass it as a parameter? I've been using C++ for over 5 years, but I haven't used a raw array since ... high-school I think (more than five years ago).
Edit: I'm using VS2010 Express.
Normally you would follow the instructions in the manual, and pass pointers to the arguments provided by main, so that gstreamer can remove the arguments which it handles.
#include <stdio.h>
#include <gst/gst.h>
int main ( int argc, char *argv[] )
{
gst_init (&argc, &argv);
// handle the remaining argc values of argv
If you want to create your own arguments, then create the same sort of array which main would have:
void gst_init(int *argc, char **argv[])
{
// strip one argument
--*argc;
++*argv;
}
void foo ()
{
int argc = 2;
char* args[] = {"myvalue1", "myvalue2"};
char** argv = args;
for(int i= 0; i < argc; ++i)
printf("%s\n", argv[i]);
gst_init(&argc, &argv);
for(int i= 0; i < argc; ++i)
printf("%s\n", argv[i]);
}
If you're not using C99, it's easier to have a separate pointer to the local array of string literals. Using C99, you could just write char** argv = (char*[]){"myvalue1", "myvalue2"}; to start with a pointer to the first element in an anonymous array.
You need to pass a pointer to a variable pointing to the array rather than a pointer to the first element in the array; in the first case the degradation of an array parameter to a pointer achieves the same effect as the second case declaring a pointer local variable - you then can pass the address of this variable and the function can modify it. sizeof( args) is 8 on a 32bit machine as the compiler deduces the number of elements in the array; sizeof(argv) is 4, therefore ++args would move the pointer to the end of the array rather than to the next element as ++argv does. The compiler protects you from such an operation.
But normally you'd use it in the way the manual suggests.
Try
int argc = 2;
char* arg1[1] = {"myvalue1"};
char* arg2[1] = {"myvalue2"};
char** argv[2] = { arg1, arg2 };
gst_init(&argc, argv);
char **argv[] is an array of char**, which is analogous to an array of char* arrays.
OTOH what you tried to pass as parameter is shown as char *(*)[2]: a pointer to an array of char*.