Throw an error/warning when supplying wrong argument type to C function

godo

I have this code:

#include <stdint.h>

void something(float a);

int main()
{
   uint8_t a = 28;
   
   something(a);

   return 0;
}

void something(float a)
{
   
   printf("%f\n", a);
}

I am using a similar function to log variables of different types to a file and I would like to get an error/warning message since I am calling function something with a wrong argument type (uint8_t instead of float).

How can I achieve this?

Lundin

The old school trick is to change the function to use pointers, since pointers in C have much stricter typing rules than integers and floating point.

#include <stdio.h>
#include <stdint.h>

void something(const float* a);

int main()
{
   uint8_t a = 28;
   
   /* gcc -std=c11 -pedantic-errors */
   something(&a); // error: passing argument 1 of 'something' from incompatible pointer type
   something(a);  // error: passing argument 1 of 'something' makes pointer from integer without a cast

   return 0;
}

void something(const float* a)
{
   printf("%f\n", *a);
}

The modern C version:

#include <stdio.h>
#include <stdint.h>

void something_float (float a);

#define something(x) _Generic((x), float: something_float)(x)

int main()
{
   uint8_t a = 28;
   
   something(a); // error: '_Generic' selector of type 'unsigned char' is not compatible with any association

   return 0;
}

void something_float (float a)
{
   printf("%f\n", a);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

block a function when argument type does not belong to a set of types

From Java

Why doesn't get() in ResultSet throw exception when 'getting' wrong type of data?

From Dev

"Error: The function applied to this argument has type ..." when using named parameters

From Dev

PostgreSQL C function get argument varchar type

From Dev

"Wrong type argument: stringp, cons" when calling apply function on concatenated lists

From Dev

vba Byref argument type mismatch when calling the function

From Dev

Function is returning wrong type C

From Dev

Specifying Function Argument by Type

From Dev

Solve wrong type argument 'cell'

From Dev

Understanding decorators: return type is a function when argument not specified

From Dev

"ByRef argument type mismatch" when passing For Each iteration Object to Function

From Dev

Error when using rref: "Undefined function for input argument of type 'cell'."

From Dev

Is it possible to make python throw errors if the type of the argument passed to the annotated function doesn't match the one specified?

From Dev

Throw an error/warning when supplying wrong argument type to C function

From Dev

Unresolved overloaded function type when attempting to pass a function as an argument

From Dev

C++ use function argument type for template argument deduction

From Dev

Why doesn't for-each method in java not throw an exception when a Function type argument is passed instead of Consumer?

From Dev

Wrong function type declaration

From Dev

Extending type of function argument

From Dev

"Error: The function applied to this argument has type ..." when using named parameters

From Dev

wrong constructor argument type, PySide

From Dev

foreach "Invalid argument supplied" when supplying array

From Dev

Wrong type of error when testing javascript function

From Dev

Unresolved overloaded function type when attempting to pass a function as an argument

From Dev

Error when passing function as an argument in C++

From Dev

Wrong argument number to recur function

From Dev

how to prevent a segfault in C (when taking an argument of the wrong type)

From Dev

C# Unity Wrong argument of the function in onClick event

From Dev

C++ function template: Must use & for argument type and return type?

Related Related

  1. 1

    block a function when argument type does not belong to a set of types

  2. 2

    Why doesn't get() in ResultSet throw exception when 'getting' wrong type of data?

  3. 3

    "Error: The function applied to this argument has type ..." when using named parameters

  4. 4

    PostgreSQL C function get argument varchar type

  5. 5

    "Wrong type argument: stringp, cons" when calling apply function on concatenated lists

  6. 6

    vba Byref argument type mismatch when calling the function

  7. 7

    Function is returning wrong type C

  8. 8

    Specifying Function Argument by Type

  9. 9

    Solve wrong type argument 'cell'

  10. 10

    Understanding decorators: return type is a function when argument not specified

  11. 11

    "ByRef argument type mismatch" when passing For Each iteration Object to Function

  12. 12

    Error when using rref: "Undefined function for input argument of type 'cell'."

  13. 13

    Is it possible to make python throw errors if the type of the argument passed to the annotated function doesn't match the one specified?

  14. 14

    Throw an error/warning when supplying wrong argument type to C function

  15. 15

    Unresolved overloaded function type when attempting to pass a function as an argument

  16. 16

    C++ use function argument type for template argument deduction

  17. 17

    Why doesn't for-each method in java not throw an exception when a Function type argument is passed instead of Consumer?

  18. 18

    Wrong function type declaration

  19. 19

    Extending type of function argument

  20. 20

    "Error: The function applied to this argument has type ..." when using named parameters

  21. 21

    wrong constructor argument type, PySide

  22. 22

    foreach "Invalid argument supplied" when supplying array

  23. 23

    Wrong type of error when testing javascript function

  24. 24

    Unresolved overloaded function type when attempting to pass a function as an argument

  25. 25

    Error when passing function as an argument in C++

  26. 26

    Wrong argument number to recur function

  27. 27

    how to prevent a segfault in C (when taking an argument of the wrong type)

  28. 28

    C# Unity Wrong argument of the function in onClick event

  29. 29

    C++ function template: Must use & for argument type and return type?

HotTag

Archive