Question about the definition of a function (three points in the parameters ..)

I came across a function definition:

char* abc(char *f, ...) { } 

What do the three dots mean?

+49
c ++ c
Mar 01 '09 at 12:34
source share
4 answers

Ellipses mean that there are the following number of arguments. The place where you used them (perhaps without realizing it) is the printf family of functions.

They allow you to create functions of this style where parameters are not known in advance, and you can use the varargs functions ( va_start , va_arg and va_end ) to get specific arguments.

This link here contains a good treatise on printf using variable argument lists.

+38
Mar 01 '09 at 12:37
source share
+15
Mar 01 '09 at 12:38
source share

They are called elipsis, and they mean that a function can take an indefinite number of parameters. Your function could probably be called like this:

 abc( "foo", 0 ); abc( "foo", "bar", 0 ); 

There should be a pointer to the end of the list. This can be done using the first parameter as the printf ion (format string 0 or special delimiter, zero in the example above.

Functions with a variable number of parameters are considered bad form in C ++, since no type checks or user conversions can be performed on parameters.

+11
Mar 01 '09 at 12:37
source share

This is what is called the varargs function or variable variable in C.

What you are likely to learn is printf.

+3
Mar 01 '09 at 12:38
source share



All Articles