1

Why does this work

int main(int argc, char **argv)
{       
        cout << argv[1] << "\n";
}

but not this

int main(int argc, string *argv)
{       
        cout << argv[1] << "\n";
}  

isn't a string just a char*? Why can we use the first but not the latter? What is the difference between char** and string*

0b11001001
  • 13
  • 3
  • There is no such thing as a string in c++. String from the standard library string.h is a helper library to make dealing with a sequence of chars more easily. – Joe Davis Nov 04 '20 at 20:11
  • Does this answer your question? [Can the arguments of main's signature in C++ have the unsigned and const qualifiers?](https://stackoverflow.com/questions/1621574/can-the-arguments-of-mains-signature-in-c-have-the-unsigned-and-const-qualifi) – Aykhan Hagverdili Nov 04 '20 at 20:11
  • 1
    @NullPointer there is a standard library type called string in C++. There is nothing against an implementation allowing `int main(int argc, std::string *argv)` in the standard – Aykhan Hagverdili Nov 04 '20 at 20:11
  • 1
    @NullPointer `std::string` is defined in ``, `` is a C header (deprecated, but usable in C++) and contains helper functions for dealing with `char*`, like `strcmp` and `strlen`. – Yksisarvinen Nov 04 '20 at 20:13
  • @Yksisarvinen which is more or less what I said? – Joe Davis Nov 04 '20 at 20:16
  • @AyxanHaqverdili yes there is because std::string is defined as ```typedef basic_string string;``` – Joe Davis Nov 04 '20 at 20:17

1 Answers1

2

In command line arguments why can't we use string *arr instead of char **arr

You could if your compiler supports such parameters for main.

But the language standard doesn't require supporting such parameters, and I know of no compiler that does. So, you cannot use that because your compiler doesn't support it.

What is the difference between char** and string*

char** is a pointer to char*. char* is a pointer to char. char is a funcamental type, an integer type and a character type which represents an encoded narrow character.

string* is a pointer to string. There is no such thing as string in the global namespace at all. There is std::string which is not a pointer, nor any fundamental type but a class type instead. It is defined in the header <string> of the standard library.

isn't a string just a char*?

No, it is not. See above.

eerorika
  • 232,697
  • 12
  • 197
  • 326