Вопрос

I am reading the book Windows System Programming. In the second chapter, there is a program Cat.c , It implements the cat command of linux. The code is http://pastebin.com/wwQFp599

On the 20th line, a function is called

 iFirstFile = Options (argc, argv, _T("s"), &dashS, NULL);   

Code for Option is http://pastebin.com/QegxxFpn

Now, the parameters for option is

(int argc, LPCTSTR argv [], LPCTSTR OptStr, ...)

1) What is this "..."? Do it mean we can supply it unlimited number of arguments of type LPCTSTR ?

2)If I execute the program as cat -s a.txt

a) What will be the argc and why? b) What will be the argv and why? c) What is _T("s")? Why _T is used here? d) Why &dashS is used? It is address of a boolean most probably. But I can't understand the logic behind use of this. e) Why they have passed NULL as last parameter?

I have Basic knowledge of C programming and these things are really confusing. So kindly explain.

Это было полезно?

Решение

You have two different kinds of "variable" lists of arguments here.

First, you have the arguments passed to the program on the command line, clearly a person could invoke the program from the command line with many arguments

 cat file1 file2 file3

and so on. The main() of C programs have since the early days of C given access to the command line arguments in the variables argc and argv, argc is the count of how many arguments (3 + the name of the pogram itself in my example above) and argv is the array of the arguments (actually an array of pointers to strings) So in this case we can access argv[0], argv[1], arv[2] and argv[3], knowing to stop there because argc tells us there are four arguments.

So in your example argc will be 3, argv[0] will point to "cat", argv[1] to "-s" and argv[2] to "a.txt".

Next the function you are looking at itself takes an indefinate number of arguments as indicated by the elipses - the ...

You need to read about variable arguments. This is a language feature that was not in the earliest C language, and is considered to be a little bit advanced, hence some of your books either may not cover it, or leave until late in the book. The key point here though is that we interpretting the variable list we need to know when we have reached the end of the variable list, we don't have an "argc" equivalent. So we put a "this is the last one, stop here" value in the function call, that's the NULL you ask about.

Другие советы

1) "..." is a variable argument list as username Cornstalks pointed out. It allows functions like printf() to have a variable number of arguments but their type and number of arguments have to be specified in one of the arguments (like the formatting string for printf()). See *va_list.h* or stdarg.h.

2) a) argc is the number of arguments specified at the command line.

b) argv is the argument array, it's an array of strings.

c) The _T() is a macro, I know it as TEXT(). Basically it allows programmers to use either ASCII strings or Unicode strings at build time without having to modify the entire code. If the UNICODE macro is defined the string specified as argument to the _T() macro becomes L"string", else it becomes "string". That's why some functions have an A or a W as a last letter. For example, OutputDebugString defaults to OutputDebugStringW if UNICODE is defined and OutputDebugStringA if UNICODE is not defined. The functions that have A as a last letter in their name only accept ASCII strings while W only accepts Unicode strings. There's also a type defined for this purpose, TCHAR defaults either to CHAR or WCHAR, and there's also another entry point, i.e. _tmain().

d) &variable means the address of a variable. It is used to pass along to a function the location in the memory of the variable contents so that if the function modifies the value of the variable, the variable is modified everywhere else where it is used.

e) You'll have to look at the function prototype.

It seems to me that you have been misled to believe that starting Windows Programming is the way to go if you want to learn to program. The C and C++ programming languages are OS independent by default and you should learn the independent part first. I recommend "C Programming : A modern approach".

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top