(C++) Very Basic Questions Regarding Syntax
C++ novice here. I have some basic questions. In int main( int argc, char *argv[] )
char *argv[]
supposed to be read (or spoken out to humans)? argv[]
to a single std::string
variable? argv[]
without argc
? If yes, how? (*) I'd appreciate explanations (not code) for numbers 2-5. I'll figure out the code myself (I learn faster this way).
Thanks in advance.
(*) I know that main(char *argv[])
is illegal. What I mean is whether there's at least a way that does not involve argc
at all, like in the following expressions:
for( int i = 0; i < argc; ++i ) {
std::cout << argv[i] << std::endl;
}
and
int i = 0;
while( i < argc ) {
std::cout << argv[i] << std::endl;
++i;
}
Or
int i = 0;
do {
std::cout << argv[i] << std::endl;
++i; } while( i < argc );
1) It is supposed to be char **argv
or char *argv[]
which is a pointer to an array of characters
more commonly known as an array of strings
2) CString is the std library to manipulate C strings (arrays of characters). You cannot resize an array without reallocating, but you can change the contents of elements by referencing it by index:
for(int i = 0; i < argc; ++i)
{
//set all the strings to have a null character in the
//first slot so all Cstring operations on this array,
//will consider it a null (empty) string
argv[i] = 0;
}
3) Technically no, however they can be deleted then reallocated:
int *array = new int[15]; //array of size 15
delete[] array;
array = new int[50]; //array of size 50
4) This is one way:
string *myString;
if(argc > 0)
{
myString = new string(argv[0]);
for(int i = 1; i < argc; ++i)
myString->append(argv[i]);
}
5) Yes, according to Cubbi:
POSIX specifies the final null pointer for argv, see for example "The application shall ensure that the last member of this array is a null pointer." at pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
Which means you can do:
char *val = NULL;
int i = 0;
do
{
val = argv[i++]; //access argv[i], store it in val, then increment i
//do something with val
} while(val != NULL); //loop until end of argv array
It's an array of pointers to char.
Sort of - you can overwrite them.
Only by copying to a new array.
Write a loop and append each argv[i] to a C++ string.
Most implementations terminate the array with a NULL pointer. I can't remember if this is standard or not.
char **argv[]
Is wrong. It should be either char **argv
or char *argv[]
, not a mixture of both. And then it becomes a pointer-to-pointer to characters, or rather a pointer to c-strings, ie, an array of c-strings. :) cdecl.org is also quite helpful at thing like this.
Then, for the access, sure. Just, well, access it. :) argv[0]
would be the first string, argv[3]
would be the 4th string. But I totally wouldn't recommend replacing stuff in an array that isn't yours or that you know the internals of.
On array resize, since you're writing C++, use std::vector
, which does all the complicated allocation stuff for you and is really safe. Generally, it depends on the array type. Dynamically allocated arrays ( int* int_arr = new int[20]
) can, static arrays ( int int_arr[20]
) can't.
To copy everything in argv
into a single std::string
, loop through the array and append every c-string to your std::string
. I wouldn't recommend that though, rather have a std::vector<std::string>
, ie, an array of std::strings
, each holding one of the arguments.
std::vector<std::string> args;
for(int i=0; i < argc; ++i){
args.push_back(argv[i]);
}
On your last point, since the standard demands argv
to be terminated by a NULL
pointer, it's quite easy.
int myargc = 0;
char** argv_copy = argv;
while(++argv_copy)
++myargc;
The while(++argv_copy)
will first increment the pointer of the array, letting it point to the next element (eg, after the first iteration it will point at c-string #2 ( argv[1]
)). After that, if the pointer evaluates to false (if it is NULL
), then the loop brakes and you have your myargc
. :)
上一篇: 检查服务器是否可以通过IPv6访问?
下一篇: (C ++)关于语法的非常基本的问题