char argv: argv is a single char
char argv[]: argv is now an array of single chars (aka a string)
char* argv[]: argv is now a pointer to an array of chars (aka argv is a pointer to one string).
I think you might see already what is confusing me. When using command line arguments how does "char* argv[]" lead to argv being a pointer to an array of strings?
It can often help to read it from right to left. char *argv[] read as []argv *char or the array argv holds pointers to chars.
the other confusion is that char * is a pointer to a char but if you pass it to a a function that requires a string (char array) it will be treated as a pointer to a string.
Edit: There is no real way of knowing if a pointer to a char is just that or meant to be a pointer to a string other than reading to documentation.
Man, this is quite a tricky concept. It's like seeing 'int' but getting 'double'.
QUOTE: "the other confusion is that char * is a pointer to a char but if you pass it to a a function that requires a string (char array) it will be treated as a pointer to a string.
I am having trouble with this. You say it can be treated like a pointer to a string. I understand that, but it's not being treated like a pointer to A string, it's a pointer to an ARRAY of strings. So we have a whole bunch of strings in an array when the notation (to me) states clearly that the array should be chars (aka one single string, not an array of them).
Am I being dumb and missing something fundamental here?
You have a pointer to an array of pointers to char (actually: a pointer to a pointer to char).
A pointer points to a single object, which may or may not be the beginning of an array. In the case of char, that means either a single character or a whole string.
In the case of argv, all pointers point to the beginning of an array. argv points to the first element of an array of pointers to char and each of those pointers in the array points to the first character of a C string.
#include<iostream>
int main()
{
// So here is an array of chars (a string)
char str[] = "A string";
// If we want a pointer to that string
// we use a pointer to the first char of the array
char * str_ptr = str;
// Now we want an array of pointers to string
char * str_ptr_arr[2];
str_ptr_arr[0] = str_ptr;
std::cout << str_ptr_arr[0] << std::endl;
return 0;
}
Thanks for the help guys those last two explanations made me realize what just wasn't clicking. I was thinking that a pointer to char can only point to a single char no matter what. But a pointer to char is basically what you use to point to a string too. So where I was seeing single chars I now see strings :)