This question already has answers here:
Currying/binding with ISO C99
(5 answers)
Functional Programming (Currying) in C / Issue with Types
(4 answers)
Is there a way to do currying in C?
(6 answers)
Partially applying a function in C
(3 answers)
Closed 3 years ago.
let's assume this variadic function is printing nice logs (without knowing its implementation because we don't care)
int printLog(int bla, int foo, const char *format, ...);
in the following code how do I affect the arguments?
void myFunction(int param)
{
typedef int (*myPrint) (const char *, ...);
if(param > 0)
{
myPrint = printLog; //how do I pass half of the argument here?
}
else
{
myPrint = printLog(1,2,...); //this is what I would like
}
//then use it
myPrint("Hello World");
unsigned int blah=1;
myPrint("blah is %d",blah);
}
Related
This question already has answers here:
Enum in C is not throwing an error on invalid input
(3 answers)
Can enums be considered unsafe? [duplicate]
(1 answer)
Closed 1 year ago.
If I have a function that takes an enum parameter, and I pass a literal that does not represent a valid enum member, can I get the compiler to emit a warning (gcc, clang, msvc?) Example code:
typedef enum {
GPIO_0,
GPIO_1,
GPIO_2,
GPIO_3
} gpio_t;
void gpio_set(gpio_t gpionum, uint8_t value) {
// ...
}
int main(int argc, const char** argv) {
// I want a warning here because there is no 5 in gpio_t:
gpio_set(5, 1);
}
This question already has answers here:
strange function definition in Scilab<->C interface
(4 answers)
Alternative (K&R) C syntax for function declaration versus prototypes
(5 answers)
Closed 3 years ago.
How Does C Allows declaring type of argument after Closed Bracket like below Code?. The below Code is actually compiling and Executing without any error.
How does this generally Work?
#include <stdio.h>
void print_int(num)
int num;
{
printf("\n\n\nNumber : %d\n\n",num);
}
int main(argc,argv)
int argc;
char** argv;
{
print_int(2);
return 0;
}
This question already has answers here:
Pointer to literal value
(12 answers)
Closed 4 years ago.
Given the following piece of C code:
void calc(int *value)
{
// do something with value
}
int main(void)
{
int i;
i = 10;
calc(&i);
}
Is it possible to get rid of setting up i and pass directly 10 to function calc? If yes, how can this be done?
Example of what I have in mind (which doesn't work):
calc( (int *) 10);
Nope, it's impossible to have a pointer to a constant in C.
UPD: however it seems that there is a trick using a struct compound literals syntax (thanks to #antti-haapala for the correction). Try this:
calc(&(int){10});
This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Returning a C string from a function
(15 answers)
Returning string from C function
(8 answers)
Return char[]/string from a function [duplicate]
(5 answers)
How can I return a character array from a function in C?
(6 answers)
Closed 5 years ago.
I would like to know how can I return a string in c and then send it as a parameter to another funcion. For example, in those functions:
This is the function that returns a char *:
char * read(){
char c[4];
printf("Write the string:\n");
scanf(" %[^\n]", c);
return c;
}
And this one takes it as a parameter and prints it:
void printc(char *a){
printf("%s", a);
}
I've tried doing:
printc(read());
But it doesn't work. What's the best way to do this?
This question already has answers here:
Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?
(5 answers)
Closed 7 years ago.
Using typedef for functions, like in the following example, are there any differences between using the address of the function or only the function?
#include <stdio.h>
int foo(int i){return i+1;}
typedef int(*mytype[2])(int);
int main(void)
{
mytype f;
f[0] = foo;
f[1] = &foo;
printf("%d %d", f[0](5), f[0](6));
return 0;
}
In the C language, functions are implicitly converted to function pointers in most contexts. Thus you see no difference between f[0] = foo and f[1] = &foo. I prefer the latter convention, but really, both are equally fine.