Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
When you want to retreive command line arguments sent to a program in C++, you say
int main(int argc, **char argv)
{
// code here
}What I am wondering is why is there a variable that holds the number of arguments sent to the program when that value can be found with argv.length?
Second (forgive me, I am still learning) what does **char variableName mean?

argc holds your command line entries whereas argv[] hold what is in those entries, so the length of the string in argv[] isn't what you would want. As an example
myprogram.exe
argc = 1
argv[0] = myprogram.exemyprogram.exe somefile.ext /s
argc = 3
argv[0] = myprogram.exe
argv[1] = somefile.txt
argv[2] = /sThe practical use depends on your program
if(argc < 3)
{
cout [[ "syntax: myprogram.exe somefile.ext /option";
return -1;
}**char is a pointer to the pointer argv[] so that you don't use the array form argv[]. I *think* but I don't use that form.
Try this with a few different command line entries.
int i;
for(i = 0; i [ argc; i++)
cout [[ "argc: " [[ i [[ " argv: " [[ argv[i] [[ endl;

Thanks for the reply.
Could you not say
for(i = 0; i < argv.length; i++)
?I still don't see what the difference is between argc and argv.length.

Unless something has changed since I learned C++, argv.length doesn't exist. argv is of type "char **", it is not an object. Have you tried to use argv.length in a program?? I'll bet that it won't compile.

I dunno if argv.length exists or not (maybe it does after the ansi 99 updates.) I'd just assume use strlen and strcpy for that sort of thing.
I don't suspect that argv.length would contain how many entries there were, but how many characters are in that entry in the way that it looks like you are trying to use it.
If you don't mind, post an example of how you're using argv.length

I now see that the length() method is a part of a class called "apstring" which I used in my computer science class. I also see that this does not work with arrays.
Nevermind, you guys were right. I didn't realize that this was the case. I see why they have that variable.
Also, it was char **argv instead of **char argv.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |