Hi All.
I m a C++ programmer and I m trying to understand a piece of code.
It fetches the values through command line arguments and makes a decision on the basis of these values. This code is working well but I dont know how?

for (int i = 0; i < argc; i++)

if (strnicmp(argv[i],"/id",3) == 0)
         strncpy(ID,argv[i] + 3,40);    //ID is defined as a char array of size 40

kindly tell me how does it work...?

9 Years
Discussion Span
Last Post by Masood Ali

argc is the number of strings in the argv array. argv[0] is always the name of the executable program and will never ever contain a command-line argument.

>>argv + 3
all that does is bypass the first 3 characters of the ith string.

The algorithm you posted doesn't work at all if you put a space between "/id" and whatever is supposed to follow. For example, if you typed this:

c:\myprogram /id something <Enter key>

In the above the program will get two command-line arguments, not one. argv[1] will be "/cd" and argv[2] will be "something". And that will break the algorithm you posted.


Thanks for the help.
Would You please tell me that why it will not work for space, if i give arguments as

c:\myprogram /idTheAdminId /pwTheAdminPw
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.