Take the 2-minute tour ×

im not a c/c+ programmer ( i do know delphi), anyway im trying to compile a program written in c++, i'v changed it to accept some arguments( a path to a file, which is hardcoded in the original code) from command line, the orignial line was

char Filepath[50] = "F:\\mylib\\*.mp3";

and i changed it to

char Filepath[50] = argv[1];

but i got "cannot convert from 'char *' to 'char []'" error, the main function is like


what should i do?? im using MSVC6.


share|improve this question
TCHAR* envp[] and char Filepath are the same things.envp is equal to a char ** but Filepath is char *... so you can convert directly –  StevenWung Nov 4 '09 at 7:28
add comment

4 Answers

up vote 5 down vote accepted


char *Filepath = argv[1];

There's no need to allocate space for 50 characters when argv[1] already contains the string you want. Also, you don't have to decide what will be the maximum number of characters in the command line argument; that space is already allocated for you.

Note, however, that the above will not make a copy of the string, so if you intend to modify the string (perhaps by appending an extension or anything), then you will have to use strcpy() or similar solution. Handling strings in C is a lot more manual character-copying work than it is in Delphi.

share|improve this answer
thanks, i strcpy worked –  avar Nov 4 '09 at 5:00
If strcpy() worked, and you copied a command line argument into a 50 byte buffer, you have a buffer overflow waiting to happen. Make sure you check the length of the string and fail if it too long, or use a function like strdup(). –  janm Nov 4 '09 at 5:10
add comment
char Filepath[50] = argv[1];

In order to copy a string as in the above example you need to use strcpy (or any of its variants)

Another (better) way is to use C++ strings

std::string Filepath = argv[1];

that would copy the string as well.

share|improve this answer
or what Greg said :-) –  Claptrap Nov 4 '09 at 5:02
This is better than what Greg said because it goes into std::string ... –  janm Nov 4 '09 at 5:11
Why is that better, janm? For all we know the rest of the code uses char*. –  Pod Nov 4 '09 at 10:58
Because there won't be overflow with a std::string, of course. –  Matthieu M. Nov 4 '09 at 13:06
its 'better' in the sense that it works regardless of the string length of argv[1] –  Claptrap Nov 5 '09 at 0:12
add comment

strncpy(Filepath, argv[1], sizeof Filepath - 1)

Or, "what Greg said".

share|improve this answer
Surely it's better to fail than silently truncate a pathname ... and in this case you need to remember termination if the string is too long ... –  janm Nov 4 '09 at 5:13
If Filepath is global or static then it will always be null-terminated. I suppose it could be auto, then he should declare it as ` char Filepath[50] = { 0 };, so the strncpy()` call will always leave a null-terminated string. Anyway, it will fail soon enough, it's just a question of when. –  DigitalRoss Nov 4 '09 at 5:32
Even if the string is null terminated (and we don't know how it was allocated), it will still be truncated and will still give the wrong result. –  janm Nov 4 '09 at 7:15
add comment

Besides using strncpy() or std::string, it might be good to see why it goes wrong. I would recommend checking this faq to see the ins and outs of pointers and arrays. http://c-faq.com/aryptr/index.html

share|improve this answer
add comment

Your Answer


