Warn if invalid value for an enum is passed? [duplicate] - c

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);
}

Related

return fields of a structure from a function in C [duplicate]

This question already has answers here:
Can't assign string to pointer inside struct
(1 answer)
Assign string to element in structure in C
(5 answers)
Closed 12 months ago.
I have following structure in C and trying to assign a string into title field of menuButtons structure. But I getting an error:
Error C2440 "=": unable to convert from "const char [6]" to "char *"
struct menuButtons {
int xTopLeft;
int yTopLeft;
int xBottomRight;
int yBottomRight;
char *title;
} easy, medium, hard;
struct menuButtons getEasyInfo() {
easy.xTopLeft = 190;
easy.yTopLeft = 200;
easy.xBottomRight = 450;
easy.yBottomRight = 280;
easy.title = "EASY MODE"; --> error
return easy;
}

How macro could be used to calculate offset of fields in a structure? [duplicate]

This question already has answers here:
How does the C offsetof macro work? [duplicate]
(4 answers)
Why does this implementation of offsetof() work?
(8 answers)
Own offsetof implementation produces warning
(1 answer)
Closed 2 years ago.
I have defined a macro that calculates the offset of a structure to any of the structure field. The Code is as follow:
#define offset(struct_name, fld_name) \
(unsigned int)&(((struct_name *)0)->fld_name)
typedef struct emp_{
char name[20];
unsigned int salary;
char designation[30];
unsigned int emp_id;
} emp_t;
int main(int argc, char **argv){
unsigned int offsetValue;
offsetValue=offset(emp_t,salary);
printf("field = %s\n","salary");
printf("offset = %d\n", offsetValue);
return 0;
}
how exactly the #define offset macro is calculating the offset? What is &(((struct_name *)0)->fld_name) actually doing? The outer & denote the address but what exactly remaining (((struct_name *)0)->fld_name) means?

variadic function pointer points to a function with preused arguments [duplicate]

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);
}

How C allows argument definition after Close baracket( ")" )? [duplicate]

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;
}

Pass pointer to a literal direct to a function [duplicate]

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});

Resources