/* va_arg example */
#include <stdio.h>
#include <stdarg.h>
void PrintLines ( char* first, ...)
{
char* str;
va_list vl;
str=first;
va_start(vl,first);
do {
str=va_arg(vl,char*);
if
printf ("%s\n",str);
} while (str!=NULL);
va_end(vl);
}
int main ()
{
PrintLines ("First","Second","Third","Fourth",NULL);
return 0;
}
Can we call the PrintLines function like this PrintLines("First","Second","Third",6,NULL); having integer as part of the variable argument list. If yes can anyone please explain how to do that?
so for your case you just would do it hardcoded, like:
void PrintLines ( char* first, ...)
...
str1=va_arg(vl,char*);
str2=va_arg(vl,char*);
str3=va_arg(vl,char*);
int4=va_arg(vl,int);
va_end(vl);
}
But I think that's not what you want: You sometimes may want to call PrintLines with an integer at pos 4, and sometimes with a string. Then you have to tell it what that thing at pos 4 is, because how should this poor function find out wether 112312123 is a integer or a address of a string?
So you have to supply some type-info to this function, maybe similar like it's done in printf and friends: The first arg contains a string describing the rest of the arguments. Maybe something like vsprintf will do a perfect job for you?
Consider variadic templates in C++ 11. I know it can do this, but never used it before.
Related
I wonder how can I tease out an array in C to several arguments of a function.
After I saw the amazing syntatic sugar from Go (golang) I thinking about it.
The c code:
#include <stdio.h>
#include <stdarg.h>
// assert: all args are valid ints
void printEach(int len, ...) {
// Irrelevant, this function print their arguments
// And I know how to use va_start, va_arg...
}
void handleVet(int* v, int n) {
// **HERE is my the question!!!**
printEach(n, v[0]...[n]) // <----------- NON-C code. I need it.
}
int main(void) {
int v[] = {12,14,15,15};
//I can do that only because the vector is static. I know the len when I'm coding
printEach(4, v[0],v[1],v[2],v[3]);
// But if we imagine an arbitrary vector, we got a problem
handleVet(v, 4);
return 0;
}
By Example, in go it would be:
package main
import "fmt"
func printEach (v ...int64) {
// Irrelevant, this function print their arguments
}
func main() {
var arr []int64 = []int64{1,14,15,}
printEach(arr...)
}
How can I achieve the same effect of "printEach(arr...)" in C?
This is a rudimentary example on how vararg is working in C.
I wasn't able to take refference to your go example as I don't udnerstand what your code does. I hope this minimal example is clear enough. If you have any questions ask me and I will edit it in.
void Foo(size_t sizeParamAmount, char *types, ...);
void Foo(size_t sizeParamAmount, char *types, ...)
{
size_t i;
float fTmp;
int iTmp;
va_list vlArgList;
va_start (vlArgList, sizeParamAmount);
for (i= 0; i< sizeParamAmount; i++)
{
switch (types[i])
{
case 'i':
iTmp = va_arg (vlArgList, int));
break;
case 'f':
fTmp = va_arg (vlArgList, float));
break;
default:
return;//error
}
}
va_end(vlArgList);
}
After reading your edit:
As I already did in my minimal example, you can hand in a pointer before the var_arg's which is explaining which argument is of what type. so you could call Foo this way:
Foo (3, "ifi", 3, 5.973, 92);
And after reading your comment to another answer I got what you are asking about.
In that case you really jsut should hand in a pointer (or array without [] behaves for this case the same) which holds an end content token.
Anyway there is a way. but you had to freak around with preprocessing tokens.
And would be totally over the top for your needs. This answer would anyway give you the requested notation. you had to set for PRED a limit by sizeof(yourarray) and the let OP take the single elements.
https://stackoverflow.com/a/10542793/2003898
But there is sadly not a more minimal example.
You will need to specify the size of the array. Here's what it might look like:
void printEach(int* values, int size)
{
if(size==0)
return;
printf("%d", value[0]);
printEach(values+1, size-1);
}
You are looking for Variadic function, you should look at
stdarg.h and varargs.h
I'm relatively new to the C programming language, and I'm trying to figure out how to create a function that can accept different types of data as parameters. The function is supposed to count and return the number of elements in a character or integer array. I already have two separate functions that will do this, but I would really like to be able to use one function for both tasks. Is there a way to do this in C?
Thanks in advance!
There is no standard function overloading in C (nor are there templates), but you could probably look into "printf-like" functions (or variadic functions) and maybe they can do what you need. If anything they allow for a flexible parameter list.
There is an example here of such a function that takes a variable size integer array.
Perhaps you could have a function signature such as void iterate(const char* format, ...); that you use in the following ways:
iterate("char", some_char_array); // for char arrays/strings
Or
iterate("int", some_int_array); // for integer arrays
Aniket makes a good point though, how do you count the elements in an integer array? If you pass an int array as an argument, you would need to pass the size too which defeats the purpose of counting the elements in the array (as you already know that i.e. the size).
I assume you don't know the size but you have a terminator value in the array (such as -1).
I've hacked something quick that kinda does what you need with the above assumption in mind.
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
int iterate(const char* format, ...)
{
va_list ap;
va_start(ap, format);
if (strcmp(format, "char") == 0)
{
char* array = va_arg(ap, char*);
va_end(ap);
return strlen(array);
}
else if (strcmp(format, "int") == 0)
{
int j = -1;
int* int_array = va_arg(ap, int*);
while (int_array[++j] != -1)
;
va_end(ap);
return j;
}
va_end(ap);
return 0;
}
int main()
{
printf("%d\n", iterate("char", "abcdef"));
int arr[] = {5, 4, 3, 2, 1, 0, -1};
printf("%d\n", iterate("int", arr));
return 0;
}
This prints:
$ ./a.out
6
6
So, let's assume your two functions are called sizeof_char_array and sizeof_int_array.
In C11, there is a new feature called "generic selection" that will let you do what you want with a relatively simple macro:
#define sizeof_array(X) \
_Generic (*(X), \
char: sizeof_char_array, \
default: sizeof_int_array) (X)
(I don't even have a C11 implementation to test this against, so caveat emptor!)
Prior to C11, this was sometimes accomplished with a macro using regularly named functions. You can define a macro that will call one function or the other depending on a macro argument hint:
#define sizeof_array(xtype, x) sizeof_ ## xtype ##_array(x)
int a[] = { 1, 2, 3, 4, -1 };
char b[] = "abc";
sizeof_array(int, a); /* macro expands to sizeof_int_array(a) */
sizeof_array(char, b); /* macro expands to sizeof_char_array(b) */
If the input argument is truly an array, you can use a macro to compute its size directly:
#define ARRAY_SZ(x) (sizeof(x)/((void *)x == &x ? sizeof(x[0]) : 0))
In the case of an array, the following expression is true:
(void *)arr == &arr
Because the address of an array has the same location in memory as the address of its first element.
So, the macro computes: sizeof(arr)/sizeof(arr[0]). Since the sizeof operator reports the size in bytes of its argument, the computed expression results in the number of elements in the array. However, if you are using a sentinel to compute the length, the ARRAY_SZ macro will result in a size at least one larger than the length found traversing the array for the sentinel.
In the case that the argument is not an array, then the expression results in a divide by 0 exception.
The answer is quite simple. You do need a function for this task. Just try this piece of code
#define len(array) sizeof(array)/sizeof(*array)
and that's it.
Important note: As pointed out in the comments, this will not work for dynamically allocated arrays.
You should make your function arguments take in a void * type. This way, you can pass in different types of data, and type-cast it to the one you want. However, you do need to watch out because there is no guaranteed way to correctly 'guess' the type that a void* points to.
In either case, you will need some sort of type-inferencing system to tell the C compiler which function to call. Which means, you will need to know, before-hand the type of array you might send in as a parameter to this "super function" of yours.
There is no "auto-type-inferencing" in C that can let you reflect upon the type of data at runtime. Better yet, you might have to write your own runtime environment for this to happen.
A slightly trivial hackish way to do this:
#include <stdio.h>
size_t GetLengthOfArray(size_t sizeOfOneElementInArray, size_t sizeOfTheArrayInBytes)
{
return sizeOfTheArrayInBytes/sizeOfOneElementInArray;
}
int main(int argc, char *argv[])
{
char cArr[10] = {'A','B','C','D','E','F','G','H','I','J'};
int iArr[5] = {10,20,30,40,50};
printf("%d is the length of cArr\n%d is the length of iArr",GetLengthOfArray(sizeof(cArr[0]),sizeof(cArr)),
GetLengthOfArray(sizeof(iArr[0]),sizeof(iArr)));
return 0;
}
It's not really possible, but you can make a tagged union
typedef struct {
union {
ssize_t i;
double d;
char *s;
} unknown;
char identity;
} Dynamic;
Or you can use a void pointer:
typedef struct {
void *unknown;
char identity;
} Dynamic;
When learning C I see that printf can receive many arguments as it is passed.
And I don't know how C can implement a function like this, where the user can type as many parameters as the user wants. I have thought about pointers, too but still have no bright idea. If anyone has any ideas about this type of function, please tell me.
You need to use va_args, va_list and the like.
Have a look at this tutorial.
http://www.cprogramming.com/tutorial/c/lesson17.html
That should be helpful.
You have to use the ... notation in your function declaration as the last argument.
Please see this tutorial to learn more: http://www.cprogramming.com/tutorial/c/lesson17.html
#include <stdarg.h>
#include <stdio.h>
int add_all(int num,...)
{
va_list args;
int sum = 0;
va_start(args,num);
int x = 0;
for(x = 0; x < num;x++)
sum += va_arg(args,int);
va_end(args);
return sum;
}
int main()
{
printf("Added 2 + 5 + 3: %d\n",add_all(3,2,5,3));
}
You use C varargs to write a variadic function. You'll need to include stdargs.h, which gives you macros for iterating over an argument list of unknown size: va_start, va_arg, and va_end, using a datatype: va_list.
Here's a mostly useless function that prints out it's variable length argument list:
void printArgs(const char *arg1, ...)
{
va_list args;
char *str;
if (arg1) We
va_start(args, arg1);
printf("%s ", arg1);
while ((str = va_arg(argp, char *)) != NULL)
printf("%s ", str);
va_end(args);
}
}
...
printArgs("print", "any", "number", "of", "arguments");
Here's a more interesting example that demonstrates that you can iterate over the argument list more than once.
Note that there are type safety issues using this feature; the wiki article addresses some of this.
I have a function, and I want to pass an array of char* to it, but I don't want to create a variable just for doing that, like this:
char *bar[]={"aa","bb","cc"};
foobar=foo(bar);
To get around that, I tried this:
foobar=foo({"aa","bb","cc"});
But it doesn't work. I also tried this:
foobar=foo("aa\0bb\0cc");
It compiles with a warning and if I execute the program, it freezes.
I tried playing a bit with asterisks and ampersands too but I couldn't get it to work properly.
Is it even possible? If so, how?
Yes, you can use a compound literal. Note that you will need to provide some way for the function to know the length of the array. One is to have a NULL at the end.
foo((char *[]){"aa","bb","cc",NULL});
This feature was added in C99.
You need to declare the type for your compound literal:
foobar = foo((char *[]){"aa", "bb", "cc"});
Some times variable arguments are sufficient:
#include <stdarg.h>
#include <stdio.h>
void func(int n, ...)
{
va_list args;
va_start(args, n);
while (n--)
{
const char *e = va_arg(args, const char *);
printf("%s\n", e);
}
va_end(args);
}
int main()
{
func(3, "A", "B", "C");
return 0;
}
But I generally prefer the method suggested by Matthew and Carl.
PHP has a func_get_args() for getting all function arguments, and JavaScript has the functions object.
I've written a very simple max() in C
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
I'm pretty sure in most languages you can supply any number of arguments to their max() (or equivalent) built in. Can you do this in C?
I thought this question may have been what I wanted, but I don't think it is.
Please keep in mind I'm still learning too. :)
Many thanks.
You could write a variable-arguments function that takes the number of arguments, for example
#include <stdio.h>
#include <stdarg.h>
int sum(int numArgs, ...)
{
va_list args;
va_start(args, numArgs);
int ret = 0;
for(unsigned int i = 0; i < numArgs; ++i)
{
ret += va_arg(args, int);
}
va_end(args);
return ret;
}
int main()
{
printf("%d\n", sum(4, 1,3,3,7)); /* prints 14 */
}
The function assumes that each variable argument is an integer (see va_arg call).
Yes, C has the concept of variadic functions, which is similar to the way printf() allows a variable number of arguments.
A maximum function would look something like this:
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
static int myMax (int quant, ...) {
va_list vlst;
int i;
int num;
int max = INT_MIN;
va_start (vlst, quant);
for (i = 0; i < quant; i++) {
if (i == 0) {
max = va_arg (vlst, int);
} else {
num = va_arg (vlst, int);
if (num > max) {
max = num;
}
}
}
va_end (vlst);
return max;
}
int main (void) {
printf ("Maximum is %d\n", myMax (5, 97, 5, 22, 5, 6));
printf ("Maximum is %d\n", myMax (0));
return 0;
}
This outputs:
Maximum is 97
Maximum is -2147483648
Note the use of the quant variable. There are generally two ways to indicate the end of your arguments, either a count up front (the 5) or a sentinel value at the back.
An example of the latter would be a list of pointers, passing NULL as the last. Since this max function needs to be able to handle the entire range of integers, a sentinel solution is not viable.
The printf function uses the former approach but slightly differently. It doesn't have a specific count, rather it uses the % fields in the format string to figure out the other arguments.
In fact, this are two questions. First of all C99 only requires that a C implementation may handle at least:
127 parameters in one function
definition
127 arguments in one function call
Now, to your real question, yes there are so-called variadic functions and macros in C99. The syntax for the declaration is with ... in the argument list. The implementation of variadic functions goes with macros from the stdarg.h header file.
here is a link to site that shows an example of using varargs in c Writing a ``varargs'' Function
You can use the va_args function to retrieve the optional arguments you pass to a function. And using this you can pass 0-n optional parameters. So you can support more then 2 arguments if you choose
Another alternative is to pass in an array, like main(). for example:
int myfunc(type* argarray, int argcount);
Yes, you can declare a variadic function in C. The most commonly used one is probably printf, which has a declaration that looks like the following
int printf(const char *format, ...);
The ... is how it declares that it accepts a variable number of arguments.
To access those argument it can uses va_start, va_arg and the like which are typically macros defined in stdarg.h. See here
It is probably also worth noting that you can often "confuse" such a function. For example the following call to printf will print whatever happens to be on the top of the stack when it is called. In reality this is probably the saved stack base pointer.
printf("%d");
C can have functions receive an arbitrary number of parameters.
You already know one: printf()
printf("Hello World\n");
printf("%s\n", "Hello World");
printf("%d + %d is %d\n", 2, 2, 2+2);
There is no max function which accepts an arbitrary number of parameters, but it's a good exercise for you to write your own.
Use <stdarg.h> and the va_list, va_start, va_arg, and va_end identifiers defined in that header.
http://www.kernel.org/doc/man-pages/online/pages/man3/stdarg.3.html