The following code consists of 2 functions: one called Options and the other called checkOptions. The Options function is fine. No problem, it is taken from Johnson M. Hart's book. The second function was added on to attempt to complete an exercise at the end of the chapter. Okay, the first function processes option flags entered on the command line as argv[1]. The second function is supposed to check to make sure all the letters in argv[1] correspond to valid option flags. The valid option flags are in the string OptStr.

It seemed like an easy exercise... But I've been trying to decipher these errors for a couple of hours now. I've been on Google and MSDN and cannot understand what is wrong.

Here's the code, but I break down the warnings/errors just after it all:

/* Utility function to extract option flags from the command line.  */

#include "EvryThng.h"
#include <stdarg.h>

void checkOptions(LPCTSTR, LPCTSTR);
DWORD Options (int argc, LPCTSTR argv [], LPCTSTR OptStr, ...)

{
	va_list pFlagList;
	LPBOOL pFlag;
	int iFlag = 0, iArg;

	va_start (pFlagList, OptStr);

	while ((pFlag = va_arg (pFlagList, LPBOOL)) != NULL
				&& iFlag < (int)_tcslen (OptStr)) {
		*pFlag = FALSE;
		checkOptions(argv [1], OptStr );
		for (iArg = 1; !(*pFlag) && iArg < argc && argv [iArg] [0] == '-'; iArg++)
			*pFlag = _memtchr (argv [iArg], OptStr [iFlag],
					_tcslen (argv [iArg])) != NULL;
		iFlag++;
	}

	va_end (pFlagList);

	for (iArg = 1; iArg < argc && argv [iArg] [0] == '-'; iArg++);

	return iArg;
}

void checkOptions( flStr, OptStr)
{
	int iArg = 1, x = 0, i = 0;
	TCHAR eMsg[0x200];

	i = _tcslen(flStr);
	for (x = 1; x < i; x++)
	{
		if (_tcschr(OptStr, (flStr)[x]) == NULL)
		{
			wsprintf(eMsg, _T(" %c is not a valid option."), flStr[x]);
			ReportError(eMsg, 0, TRUE);
		}
	}
	
}

The first two warnings concern this line:

i = _tcslen(flStr);

at one point I had this function inserted in the for statement following it. But I put it on a line like this to see if I couldn't alter the warning. No luck. The two warnings are:

warning C4047: 'function' : 'const wchar_t *' differs in levels of indirection from 'int'
warning C4024: 'wcslen' : different types for formal and actual parameter 1

Okay. flStr is declared LPCTSTR which is a const wchar_t* with Unicode turned on. But this is in a function that returns the number of characters in a string. A number! So what the heck do I do about that?

The second warning: Well MSDN describes the function so:

size_t wcslen(
   const wchar_t *string 
);

That's what that first parameter is in my function.

The next 2 warnings and 2 errors concern this line:

if (_tcschr(OptStr, flStr[x]) == NULL)

warning C4047: 'function' : 'const wchar_t *' differs in levels of indirection from 'int'
warning C4024: 'wcschr' : different types for formal and actual parameter 1
Same deal as before. (I'm compiling with Unicode on). Except this time I don't even see an int. The function is supposed to return NULL if the character in flStr[x] is not found in OptStr.

error C2109: subscript requires array or pointer type
error C2198: 'wcschr' : too few arguments for call

I'm thinking the second error would disappear if I could fix the first. Again, flStr is a LPCTSTR, therefor a pointer type. I can't see why this is any different than the way the first function subscripted the string by using argv[Arg1][0]

Next error for this code:

wsprintf(eMsg, _T(" %c is not a valid option."), flStr[x]);

error C2109: subscript requires array or pointer type

Same as above.

So 4 warnings and three errors. But the warnings are in like pairs and two of the errors concern the same misconception and I'm hoping the last error will disappear if I can fix the error before it. So, it really comes down to two warnings and (hopefully) one error.

I'm a hopeful kind of guy:icon_razz:

Recommended Answers

All 2 Replies

Did you typedef LPCTSTR to flStr and OptStr? If not your function should look like

void checkOptions(LPCTSTR  flStr,LPCTSTR  OptStr)
{
	int iArg = 1, x = 0, i = 0;
	TCHAR eMsg[0x200];

	i = _tcslen(flStr);
	for (x = 1; x < i; x++)
	{
		if (_tcschr(OptStr, (flStr)[x]) == NULL)
		{
			wsprintf(eMsg, _T(" %c is not a valid option."), flStr[x]);
			ReportError(eMsg, 0, TRUE);
		}
	}
	
}

Warning should go once you typecast the variables appropiatly.

dubeyprateek Thankyou so much. I cannot believe that's what it was. Actually, I did put that in when I first typed the code but took it back out because I thought once I declared the types in the function prototype it was taken care of in the definition. Blind me. I was getting to feel like I was never going to learn this language.

Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.