Вопрос

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