This question already has answers here:
Macro returning the number of arguments it is given in C? [duplicate]
(3 answers)
Closed 4 years ago.
It might be impossible to do but can we actually get the number of arguments a function's wants in C?
The goal is to make a function named call that requires a function's pointer and some arguments. But because we can't count the number of arguments in a variadic arguments function, I want to count how many arguments does the function's pointer requires.
Here's how I want to create it:
void func(int a, int b, char* c) {
// Do things
}
int call(void *f, ...) {
// Find how much arguments *f wants and loop over the variadic args
}
int main() {
call(func, 1, 2, "hello");
}
By the way, it needs to be at run-time.
Nothing in the C standard provides a facility for ascertaining the number of arguments a function requires, other than the fact that C implementations are free to provide extensions.
Related
This question already has answers here:
An example of use of varargs in C
(4 answers)
Closed 3 months ago.
I'm trying to understand libv4l2, & came across with following function that contains three dots in the argument part. What does this ... mean?
int v4l2_ioctl (int fd, unsigned long int request, ...)
{
void *arg;
va_list ap;
int result, index, saved_err;
int is_capture_request = 0, stream_needs_locking = 0;
Also, my C knowledge is poor. So please, Explain Like I’m 5
I'm expecting to understand that does ... meant in a function,
I've done basic search about these three-dots, but only found something related with C's Macro (not good results with something related with C functions).
My long-term goal is to understand about this v4l2-ioctl function, (not like understand in and out) but understand briefly so I can use this v4l2-ioctl comfortably.
its a variadic function- a function that can take a variable number of arguments
you have to access them with va_start, va_arg and va_end
if u want to read more about it u can here:
http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html
This question already has answers here:
Is it better to use C void arguments "void foo(void)" or not "void foo()"? [duplicate]
(6 answers)
Closed 6 months ago.
So I get that a void function won't return a value, while a int one for example will return an integer. So void main(){} doesnt return, but int main(){return 0;} will return.
My question is, what is the difference between these 3 functions. I know the first one wont return a value, the second one will return an integer. But how about the third one? I know it returns an integer because I've tested it, so what does that (void) does? Why is is there?
void main(){}
int main(){return 0;};
int main(void){return 0;};
I'm a begginer so sorry if it sounded confusing... Thanks in advance!!
(The question is about the programming language C)
Between the parenthesis are the parameters to the function.
In a function definition, the proper way to designate a function that takes no parameters is to simply specify void for the parameter list.
An empty parameter list (in a definition) is also a way of saying the function takes no parameters, however this syntax is considered deprecated and should not be used in new programs.
This question already has answers here:
How do Variable length arguments work in C?
(6 answers)
Closed 1 year ago.
If I have a C function where one argument is "...", how do I determine if more than two arguments are being passed to that function? If the third argument being sent to my_func() would be int arg2, how do I access it?
#include <stdio.h>
int my_func(int arg0, int arg1, ...)
{
/* How can I determine if 2 or 3 arguemnts are being passed to
* this function? How do I access the third argument so I could
* print it out? */
return 0;
}
int main() {
my_func(1,2);
my_func(1,2,3);
return 0;
}
In C, you can't deduce how many arguments are sent to the function from the arguments alone. You need some supporting information, usually passed in one of the arguments themselves. E.g., for the well-known printf, the first argument is a format string, and parsing it will tell the function how many other arguments it should take and their types.
This question already has answers here:
func() vs func(void) in C99
(4 answers)
Difference between int main() and int main(void)?
(10 answers)
Closed 4 years ago.
What is the difference between these two programs?
The 1st one i am getting 4,3,2,1 and 2nd one is compilation error.
#include <stdio.h>
int main()
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
and
#include <stdio.h>
int main(void)
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
When you define a function like this:
int func() { ... }
It says that the function takes an indeterminate number of arguments and returns an int. So you can legally pass any number of arguments of any type (although you won't be able to access them).
When you define a function like this:
int func(void) { ... }
It says that the function takes no arguments. Attempting to pass any arguments to this function will result in a compile time error.
On a side note, recursively calling the main function is not a good idea. You're better off either calling another function which is recursive or just using a loop.
The appearance of a solitary void in a parameter list explicitly tells the compiler "this function takes no arguments".
In the first code example, calling main recursively is permitted since there is no argument list, which permits any number of arguments (this may have been changed in a more recent C standard than the one supported by your compiler; I forget the specifics).
Variables declared static are stored in the process' data section rather than in stack memory, so they persist beyond their scope and retain their value across function calls, so i decrements on each call until it reaches zero and your program hits the base case (don't enter the if statement), and terminates.
This question already has answers here:
Passing variable arguments to another function that accepts a variable argument list
(11 answers)
Closed 9 years ago.
Is there a way to implement this?
void func2(...) {
/*
* Handle „...” parameters
*/
}
void func1(int n, ...) {
func2(...);
}
No, you can't. Variadic arguments cannot be forwarded. Your choices are:
have your "inner" function take a(n initialized) va_list argument instead of ... and pass that list from the caller;
if the arguments are of the same (or convertible) types, you can make it accept an array, then parse the variadic arguments yourself and pass the array and its length to the called function.
This is impossible according to Wikipedia. http://en.wikipedia.org/wiki/Stdarg.h
Variadic functions are functions which may take a variable number of
arguments and are declared with an ellipsis in place of the last
parameter. An example of such a function is printf. Variadic functions
must have at least one named parameter, so, for instance, char
*wrong(...); is not allowed in C. (In C++, such a declaration is permitted, but not very useful.) In C, a comma must precede the
ellipsis; in C++, it is optional.
So your void func2(...) is illegal.