This question already has answers here:
How to achieve function overloading in C?
(14 answers)
Closed 6 years ago.
Let's assume I want to create a function first that returns the first element of an array in C. Obviously I want to create something that accounts for all types.
I would start with this:
int first(int list[]) {
return list[0];
}
Which works. Obviously...
I would now like to do the same for char
char first(char list[]) {
return list[0];
}
Which does not compile as there is already a first function in the program.
How do you C guys handle this kind of scenarios?
Is one forced to go with different names?
int first_int(int list[]) {
return list[0];
}
char first_char(char list[]) {
return list[0];
}
C11 introduced generic selections to emulate overloading:
#define first(X) \
_Generic((X), \
int* : first_int, \
char*: first_char \
)(X)
See it live on Coliru
Related
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);
}
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:
How to pass a constant array literal to a function that takes a pointer without using a variable C/C++?
(10 answers)
Closed 5 years ago.
So , i'm trying to make a simple program in C using arrays.
int odd(int v1[],int n) {
int v2[n];
int i;
for (i=0;i<n;i++) {
if (v1[i]%2==0) {
v2[i]=v1[i];
}
else {
v2[i]=v1[i]*2;
}
}
for (i=0;i<n;i++) {
printf("Array %d",v2[i]);
}
return 0;
}
int main() {
odd({1,2,3,4,5},5);
return 0;
}
I get an error in the main function ('Expected expression') and i don't know how to correct.
An expression like
{1,2,3,4,5}
is not an array, on it's own. It's a brace-enclosed list, which is primarily used for initialization purpose.
If you don't want to define a separate array variable, you need to make use of compound literal. Something like
odd((int[]){1,2,3,4,5},5);
will do the job.
This question already has answers here:
C Why function pointer as parameter instead of just a function?
(2 answers)
Closed 6 years ago.
I just noticed that this compiles without any errors or warnings using -pedantic -Wall with both gcc and clang.
#include <stdio.h>
int x = 0;
void func(int f(const char *)) {
f("func()!");
}
int main(void) {
func(puts);
}
It appears that the parameter f is treated like pointer to function int (*)(const char *) in this case.
But this a behavior that I have never seen or heard anything about. Is this legal C code? And if so then what happens when you have a function as a parameter to a function?
It is allowed by the standard. From C99 standard chapter 6.9.1 (took from http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf):
EXAMPLE 2 To pass one function to another, one might say
int f(void);
/*...*/
g(f);
Then the definition of g might read
void g(int (*funcp)(void))
{
/*...*/
(*funcp)(); /* or funcp(); ... */
}
or, equivalently,
void g(int func(void))
{
/*...*/
func(); /* or (*func)(); ... */
}
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.