HELP!! ... parameter, va_list, va_start (and other macros)

So I'm trying to write a function similar to printf. The point of this function is to get each argument (via va_arg macro) from the ... parameter individually and add a directory separator (cross platform code mac/win/linux) between each parameter to build a path. I'm pretty close but the thing I can't figure out is how to determine how many calls to va_arg I can get away with. I basically want to know the size of ...

Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool CreatePath( const wchar_t* pszFormat, ... )
{
	va_list listPointer;
	va_start( listPointer, pszFormat );

	// Not sure how far to loop ??
	for( int i = 0; i < ??; i++ )
	{
		const wchar_t* pszArg = va_arg( listPointer, const wchar_t* );

		if( !pszArg ) break;

		printf( "%ls", pszArg );
	}

        va_end( listPointer );

	return 0;
}


please help!

I've already read through all this stuff: http://www.cplusplus.com/reference/clibrary/cstdarg/va_list/
Last edited on
I believe you have to either tell CeatePath() the number of arguments or use some kind of Marker to indicate the end of the argument list.

From http://www.cplusplus.com/reference/clibrary/cstdarg/va_arg/:
Notice also that va_arg does not determine either whether the retrieved argument is the last argument passed to the function (or even if it is an element past the end of that list). The function should be designed in such a way that the amount of parameters can be inferred in some way by the values of either the named parameters or the additional arguments already read.
You can do one of two things (or more probably lol):

1 - explicitly tell CreatePath() how many arguments are in the variable argument list
2 - pass some sort of format string and make decisions about how many parameters there are based on that string (e.g. if you pass "%ls %ls %d %p %x" as a format string you can just count the number of %'s and you will have your number of args in your va_list

Thanks for the response Caligulaminus! If anyone could add to this thread I would appreciate it!
Topic archived. No new replies allowed.