質問

I create a clone of the echo command and that's more complicated than I was thinking. Here my problem : How works a char *argv[]? I know how works char myString[], but no that weird way to create strings :

  • It's a pointer on an array of chars, or an array of pointers on chars?
  • Why when I *argv[n], it shows me the n argument, not the n char... Chars can ve
役に立ちましたか?

解決

char* argv[] behaves just like char** argv would behave. You could also use an char argv[][] the same way.

So basically, when you call *argv, you get argv[0] and *argv[n] gives you argv[0][n]

edit: to convert argv[x] into a std::string:

std::string s(argv[x]);

from that point on s is a standard string. But keep in mind that you must have zero terminated strings, this will not work for random binary data in a char**!

他のヒント

It's a pointer on an array of chars, or an array of pointers on chars?

In C, arrays are always passed as pointer to the first element. So, effectively you can treat it as char**, which is a pointer to pointer of char.

Why when I *argv[n], it shows me the n argument, not the n char... Chars can ve

You can split that expression according to the operator precedence:

char* p = argv[n];
char  c = *p;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top