function-pointers

Function pointer to __attribute__((const)) function?

ⅰ亾dé卋堺 提交于 2019-12-13 12:25:44
问题 How (in GCC/"GNU C") do you declare a function pointer which points to an __attribute__((const)) function? The idea being that I want the compiler to avoid generating multiple calls to the function called through the function pointer when it can cache the return value from a previous call. 回答1: typedef void (*t_const_function)(void) __attribute__((const)); static __attribute__((const)) void A(void) { } static void B(void) { } int main(int argc, const char* argv[]) { t_const_function a = A; //

Pointer to member type incompatible with object type → What is the cause?

六月ゝ 毕业季﹏ 提交于 2019-12-13 09:35:37
问题 Recently I ran into a compiler (GNU g++ 4.9.2) error like this: ProceduralTimerTaskAdapter.cpp:25:13: error: pointer to member type ‘void (Poco::Util::Timer::)(Poco::Util::TimerTask&)’ incompatible with object type ‘Poco::Util::ProceduralTimerTaskAdapter’ Here is the relevant code (which is almost self-contained, save for the necessary Poco libs): ProceduralTimerTaskAdapter.h: #include <Poco/Util/Timer.h> #include <Poco/Util/TimerTask.h> #include <Poco/Util/TimerTaskAdapter.h> #ifndef

what does the line int *(*(x[3])())[5]; do in C? [closed]

£可爱£侵袭症+ 提交于 2019-12-13 09:13:44
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . This is my first time asking a question on Stack Overflow, so please fell free to tell me if I did anything wrong or not specific enough. I've been programming microcontrollers in C for about 4 now. Some days ago I took part in an Electronics Competition. One of the many questions was what exactly the C codeline

Edit script for user input rather than internal input

允我心安 提交于 2019-12-13 08:31:17
问题 I have a simple script and it works fine. My question is, how can I get my results to display in a reduced simplified fraction? Also, at this point I am defining the values myself, is there a way to have the script ask the user to input the numerator and denominator for the different fractions used? The posted script worked wonders for me in regards towards asking and performing the operations, so I am very thankful for that. It did not reduce the fractions, am I still missing something?

Changing the address value of a function pointer [closed]

落爺英雄遲暮 提交于 2019-12-13 08:30:02
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I have the following code in C: int addInt(int n, int m) { return n + m; } int (*functionPtr)(int, int); functionPtr = &addInt; functionPtr is a function pointer and it points to the specific address of the function addInt . I want to change 1 bit of its value, but I can't figure it out how. Let's

Passing single pointer and double pointer to a function in c [closed]

馋奶兔 提交于 2019-12-13 08:26:45
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I am trying to pass a single pointer and double pointer in a function. but its giving me error. int main() { int *pt; fun(pt); . . } fun(int *pt) { pt=(int*)malloc(6*sizeof(int)); . . } and what is the syntax when we are using double pointer. Can anybody just describe it with an example or can edit

Templated Function Pointer in C++

随声附和 提交于 2019-12-13 07:49:48
问题 I am facing a problem with templated member function pointer. The code is as shown below. #include <String> #include <iostream> template<typename T> struct method_ptr { typedef void (T::*Function)(std::string&); }; template <class T> class EventHandler { private: method_ptr<T>::Function m_PtrToCapturer; }; e:\EventHandler.h(13) : error C2146: syntax error : missing ';' before identifier 'm_PtrToCapturer' I am facing this error. Even If I use method_ptr<EventHandler>::Function m_PtrToCapturer;

Exporting a function, cast into a pointer through a DLL

拈花ヽ惹草 提交于 2019-12-13 07:09:23
问题 This is an elaboration to an earlier question: How to reset state machines when unit testing C There is also a similar question but i don't the answer match my problem and i have some examples i wish to present: Exporting a function pointer from dll I have two sets of code that i expect should do the same but the latter crashes. Using mingw32 and Win7. The function to be exported. This is to be considered legacy and unmutable. addxy.c int addXY(int x, int y) { return x + y; } addxy.h int

How to get a “simple” function pointer from a member function

冷暖自知 提交于 2019-12-13 05:03:31
问题 I'm having a problem with function pointers and nothing I found on the net helped me to solve this problem. I have a function from a C API which take a pointer of a void function : extern int APIFunction(int, void (*func)(int)); I have a class with the function I would like to put when I call the API function. class MyClass { public: void myFunction(int status, otherAPi arguments...); }; Then, I created a pointer to my member function and created a new instance of my class typedef void

How to pass a member function as a parameter to a function that doesn't expect it?

安稳与你 提交于 2019-12-13 04:58:33
问题 Say I have a function foo : void foo(void (*ftn)(int x)) { ftn(5); } It needs as a parameter a void function that accepts an int as a parameter. Consider void func1(int x) {} class X { public: void func2(int x) {} }; Now foo(&func1) is ok. But foo(&X::func2) isn't ok because X::func2 isn't static and needs a context object and its function pointer type is different. I tried foo(std::bind(&X:func2, this)) from inside X but that raises a type mismatch too. What is the right way of doing this?