문제

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