function-pointers

Need help for reading a file character by character in C

帅比萌擦擦* 提交于 2021-01-29 07:08:51
问题 I have a question about reading a file character by character and counting it in C here's my code down below void read_in(char** quotes){ FILE *frp = fopen(IN_FILE, "r"); char c; size_t tmp_len =0, i=0; //char* tmp[100]; //char* quotes[MAX_QUOTES]; //char str = fgets(str, sizeof(quotes),frp); while((c=fgetc(frp)) != EOF){ if(frp == NULL){ printf("File is empty!"); fclose(frp); exit(1); } else{ if(c != '\n'){ printf("%c",c); c=fgetc(frp); tmp_len++; } } char* tmp = (char*)calloc(tmp_len+1,

Cast Between Incompatible Function Types in gcc

我们两清 提交于 2021-01-28 22:14:20
问题 I have a code generation script that was written by someone else around 2008 and has worked fine mostly unchanged since then. Just recently I tried compiling with gcc9 and I see 7300 warnings for "cast between incompatible function types" in the generated code. The code takes a set of function pointers and various type identifiers and inserts everything into a big map that's used later for option serialization, printing, etc. Many functions and variables are cast and stored as some form of

What are the semantics of function pointers with empty parentheses in each C standard?

佐手、 提交于 2021-01-28 08:22:28
问题 Answers to this and this question say that function pointers of the form return-type (*pointer)() are pointers to a function which takes any number of arguments, though the latter says they obsolesced in C11. On an i386 system with GCC, “extra” arguments passed in a call to an empty-parentheses-type’d function pointer are ignored, because of how stack frames work; e.g., /* test.c */ #include <stdio.h> int foo(int arg) { return arg; } int main(void) { int (*fp)() = foo; printf("%d\n", fp

Function Pointer Error in C++

∥☆過路亽.° 提交于 2021-01-28 04:17:45
问题 Here is the error I am getting: error: no matching function for call to ‘pcl::ConditionalEuclideanClustering <pcl::Normal>::setConditionFunction(bool (EuclideanPlaneSegmentation::*)(const pcl::Normal&, const pcl::Normal&, float))’ cec.setConditionFunction(&EuclideanPlaneSegmentation::customRegionGrowing; ^ note: candidate is:/segmentation/conditional_euclidean_clustering.h:125:7: note: void pcl::ConditionalEuclideanClustering <PointT>::setConditionFunction(bool (*)(const PointT&, const PointT

Member functions returning pointers to member functions

六月ゝ 毕业季﹏ 提交于 2021-01-27 07:05:45
问题 I'd like to have a class with member functions that return pointers to member functions. That is, something like: class Foo { // typedef ????(Foo::* func)???? public: Func s1(); Func s2(); Func s3(); } Func Foo::s1() { // do stuff return &Foo::s2; } Func Foo::s2() { // do stuff return &Foo::s3; } Func Foo::s3() { // do stuff return 0; } Basically, what I try to do is to implement a state machine, where each state nows the next state and returns it by means of a function pointer. Note: I'm not

C function name or function pointer? [duplicate]

霸气de小男生 提交于 2021-01-22 06:34:08
问题 This question already has answers here : In C, what is the difference between `&function` and `function` when passed as arguments? (4 answers) Closed 4 years ago . Let's see this code: #include <stdio.h> typedef int (*callback) (void *arg); callback world = NULL; int f(void *_) { printf("World!"); return 0; } int main() { printf("Hello, "); // world = f; world = &f; // both works if (world != NULL) { world(NULL); } } When setting world variable, both world = f; and world = &f; works. Which

Extracting function argument types as a parameter pack

只谈情不闲聊 提交于 2021-01-21 10:34:26
问题 This is a followup question to "unpacking" a tuple to call a matching function pointer, which asked how to provide the values from a std::tuple as arguments to a function in a generic way. A solution given there was the following: template<int ...> struct seq { }; template<int N, int ...S> struct gens : gens<N-1, N-1, S...> { }; template<int ...S> struct gens<0, S...> { typedef seq<S...> type; }; double foo(int x, float y, double z) { return x + y + z; } template <typename... Args> struct

automatic decay of lambda to function pointer when passing to template function

大兔子大兔子 提交于 2021-01-18 05:41:40
问题 Is there a way to make a lambda decay to a pointer, without explicitly casting to the right signature? This would tidy some code: template<typename T> T call(T(*func)()){ return func(); } int ptr(){ return 0; } int main(){ auto ret1 = call(ptr); auto ret2 = call((int(*)())([]{ return 0; })); auto ret3 = call([]{ return 0; }); //won't compile } It's evident that a call to call works only if the lambda decays to a pointer, but I'm guessing that that can happen only after the right function

automatic decay of lambda to function pointer when passing to template function

与世无争的帅哥 提交于 2021-01-18 05:34:05
问题 Is there a way to make a lambda decay to a pointer, without explicitly casting to the right signature? This would tidy some code: template<typename T> T call(T(*func)()){ return func(); } int ptr(){ return 0; } int main(){ auto ret1 = call(ptr); auto ret2 = call((int(*)())([]{ return 0; })); auto ret3 = call([]{ return 0; }); //won't compile } It's evident that a call to call works only if the lambda decays to a pointer, but I'm guessing that that can happen only after the right function

C++ “Dynamic” function pointers for C callback functions

六眼飞鱼酱① 提交于 2020-12-26 06:56:43
问题 I have an API for managing a camera configuration. There are 344 individual options to manage. When a certain value changes the API calls a callback function to notify the program. The register function takes a void RegisterCallback(Option * ptr, void (fn*)(void*)) function pointer as a callback function. I cannot use a single function for callback, because I do now where is the callback coming from . One solution is to create 344 individual callback functions: void callback0(void*); void