This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Question about a function definition (three dots in parameters..)
I'm new to c++, what is the meaning of the ... (triple dot) inside the method or function
sample
void strArr::set(const char *value, ...) {
// some code here
}
It's a way to express a variable number of arguments, used to be called va_args or variadic function.
Take a look here to learn about them in C++, they do exist in other programming languages though, since it's just a generic concept.
That's a variadic function, it can take any number of parameters (more than 1 in this case).
Same principle as printf and others:
printf("%d", 1);
printf("%d%d%d", 1,1,1);
printf("%d%d%d%d%d%d", 1,1,1,1,1,1);
These aren't overloads, but the same function taking a variable number of arguments.
Related
This question already has answers here:
Passing references to pointers in C++
(10 answers)
Closed 4 years ago.
I have two function signatures in C++
void printArray(int* arrayPtr);
void printArray(int*& arrayPtr);
I understand the 1st function. It says the function takes in a arrayPtr argument which is of type that's a pointer pointing to an integer.
Both function signature works, but I have a hard time understanding the 2nd signature(*&) and what benefits it offers?
It's exactly the same as type versus type&; the first is a value and the second is a reference. The fact that type is a pointer doesn't change that.
This question already has answers here:
Calling a function through its address in memory in c / c++
(6 answers)
Closed 5 years ago.
I have a function at a known memory address(for example: 0x11111111). The function returns an int, and takes a uint32_t pointer as its only argument.
How would I call this function using c++? I have seen a few examples of calling a function by its address, but I can't seem to find one that takes a pointer as its argument
EDIT:I seen that. Doesn't address how to call the function that takes a pointer as an argument
If you’re sure that there’s a function there, you could call it by casting the address to a function pointer of the appropriate type, then calling it. Here’s C code to do this:
typedef int (*FunctionType)(uint32_t*);
FunctionType function = (FunctionType)0x11111111;
function(arg);
This can easily be modified to support any number of function arguments and any return type you’d like. Just tweak the argument types list of the FunctionType typedef.
Or, in one line (gulp):
(((int (*)(uint32_t *)) 0x11111111)(arg);
This question already has answers here:
Is f(void) deprecated in modern C and C++? [duplicate]
(6 answers)
Closed 6 years ago.
I have seen in C++ program, during function declaration if there is no parameter for the function void is declared as parameter like this:
int F1(void)
How is it different than:
int F1()
There is no difference. Using void is just a more explicit way to declare the same thing. Personally, I never use that syntax and rarely see anyone else use it either.
It's the same thing in C++, and is a holdover from C.
Here's an excerpt from the C++ 2003 standard (C.1.6):
Change: In C++, a function declared with an empty parameter list takes no arguments.
In C, an empty parameter list means that the number and type of the function arguments are unknown"
Example:
int f(); // means int f(void) in C++
// intf(unknown) in C
Rationale: This is to avoid erroneous function calls (i.e. function calls with the wrong number or type of arguments).
Effect on original feature: Change to semantics of well-defined feature. This feature was marked as “obsolescent” in C.
Both of them are exactly the same, leaving the argument field empty as () is the one I prefer, some prefer writing (void) just so that someone editing the code may be ensured that no arguments are required. Makes no difference though, just a readability thing.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C/C++: Passing variable number of arguments around
Imagine I have a function mySuperDuperPrintFunction that takes a variable number of arguments. This function calls printf (or any other function with a variable number of arguments. Can i somehow pass all, or only some, parameters from the arglist to the other function ? Like
void mySuperDuperPrintFunction(char* text, ...) {
/*
* Do some cool stuff with the arglist.
*/
// Call printf with arguments from the arglist
printf(text, *someWayToExtractTheArglist());
}
Yes:
va_list args;
va_start(args, text);
vprintf(format_goes_here, args);
You can find info about vprintf here (or check your man pages).
I've done similar for wrapping functions such as write (Unix) and OutputDebugString (Windows) so that I could create a formatted string to pass to them with vsnprintf.
EDIT: I misunderstood your question. No you can't, at least not in C. If you want to pass the variable number of arguments on, the function you're calling must take a va_list argument, just like vprintf does. You can't pass your va_list onto a function such as printf(const char *fmt,...). This link contains more information on the topic.
If the function does take a va_list argument, then you can pass arguments from a specific point (ie. you might to skip the first one). Retrieving arguments with va_arg will update the va_list pointer to the next argument, and then when you pass the va_list to the function (such as vprintf), it'll only be able to retrieve arguments from that point on.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In a C function declaration, what does “…” as the last parameter do?
What does this mean ,...); it is written at the end of a function in a code i am debuging.
like this void abc( int a, int b, ...);
It means the function can take any number of extra arguments. For example, consider printf; the first argument is the format string, and then there can be any number of arguments after that for all of the modifiers. This would be represented by using ... after the first argument when defining the function.
That specifies a variable number of arguments which can be accessed using the macros in the cstdarg header.