Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function parameter ...

Status
Not open for further replies.

timmay3141

Programmer
Dec 3, 2002
468
US
I was looking at some code in a Direct3D tutorial and ran across the following function used for debugging:

void WriteToLog(char *lpszText, ...)
{
if(m_fEnableLogging)
{
va_list argList;
FILE *pFile;

//Initialize variable argument list
va_start(argList, lpszText);

//Open the log file for appending
pFile = fopen("log.txt", "a+");

//Write the text and a newline
vfprintf(pFile, lpszText, argList);
putc('\n', pFile);

//Close the file
fclose(pFile);
va_end(argList);
}
}

I thought I knew C++ pretty well, but none of the books I've ever used have mentioned using ... as a function parameter. I'd seen it before in printf too, but I'd just ignored it. Anyway, can someone tell me what ... does and how it is being used?
 
it is called an elipsis i believe. The Stroustrup book covers it and most c++ books do as well. The only problem is you need to know what it is called. The first param usually describes how many args there are and their types (if applicable). then with the va_start/end you can look through them.

Hope this is of some assistance.

Matt
 
I think I got it. I'd seen the ellipsis before, but only in catch blocks. I found stuff about va_start/end in a c++ reference book, but my original c++ book doesn't mention it (it wasn't very good). Thanks for the help.
 
Yeah, it is the same syntax used by the C library's printf, sprintf, and fprintf.
 
You can easely set it up yourself without the va_arg macros.
e.g.
long Add(long argc, ...)
{
long* argv = &argc + 1;
long total=0, i=0;

while(i < argc)
total += argv[i++];

return total;
}

 
Binaryshi, your method is architecture-dependent, because some machines have upwards growing stack.
 
Thanks a lot mingis, I didn't know that.
Do you know what machines have this upwards growing stack?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top