function-pointers

typo in function pointer variable declaration, but code compiles

情到浓时终转凉″ 提交于 2019-12-12 12:12:49
问题 While answering a question about function pointers, OP did that to declare a function pointer which takes 1 integer argument and returns nothing: void *(intr_handlerptr)(int); // wrong but compiles!! intr_handlerptr = intr_handler; // compiler error: cannot assign to this weird thing (lvalue required as left operand of assignment) when the proper declaration is void (*intr_handlerptr)(int); // correct What's funny is that the error occurs when assigning to this function pointer, not when

Algorithm Design Manual, chapter 3, linked-list code snippet confusion

那年仲夏 提交于 2019-12-12 11:41:24
问题 I'm reading the Algorithm Design Manual and in chapter 3, the following code snippet appears. It has to do with removing an item from a linked-list. The question isn't related to data-structures, but just to a single line of code where two variables are declared I think . I've stripped the non-relevant parts of the code for brevity. list *search_list(list *l, item_type x) { // This function just searches the list x } list *predecessor_list(list *l, item_type x) { // This function simply

How to get the arguments of a function pointer from a CallExpr in Clang?

浪尽此生 提交于 2019-12-12 11:33:26
问题 I am trying to analyse C source code with function calls within them. I am able to analyse normal function calls to get their arguments without problem using the source code below where ce is a CallExpr object: 1. if(ce != NULL) { 2. QualType q = ce->getType(); 3. const Type *t = q.getTypePtrOrNull(); 4. 5. if (t != NULL) { 6. llvm::errs() << "TYPE: " << t->isFunctionPointerType() << " " << q.getAsString() << " " << t->isPointerType() << "\n"; 7. } else { 8. llvm::errs() << "FUNCTION CE HAS

How compatible are static class methods and regular routine pointers?

心不动则不痛 提交于 2019-12-12 09:53:35
问题 It seems to me that static class methods and regular routine pointers are compatible from a practical viewpoint but the compiler doesn't know this. Example: type TFunc = function(i: Integer): string; TMyClass = class public class function StaticMethod(i: Integer): string; static; end; class function TMyClass.StaticMethod(i: Integer): string; begin Result := '>' + IntToStr(i) + '<'; end; function GlobalFunc(i: Integer): string; begin Result := '{' + IntToStr(i) + '}'; end; procedure CallIt

Casting function pointers

穿精又带淫゛_ 提交于 2019-12-12 09:30:55
问题 I am writing a function that receives a pointer to a comparison function and an array of MyStructs and is supposed to sort the array according to the comparison function: void myStructSort( struct MyStruct *arr, int size, int (*comp)(const struct MyStruct *, const struct MyStruct *)) { qsort(arr, size, sizeof(struct MyStruct), comp); } Unfortunately this doesn't compile because qsort expects the comparator to receive void * arguments and not const struct MyStruct * . I thought of several bad

Passing a member function as an argument to a constructor

筅森魡賤 提交于 2019-12-12 09:12:41
问题 I have a button class. I want the button class's constructor to take the function it will call when the button is pressed. This would be easy if the button class was only taking a function from one class, but the purpose of the button's constructor taking a function is that no matter what class the button is created in it will be able to store a pointer to a function in the class it's created in. To illustrate: struct Button { Button(void (*functionPtr)()) { // Store the function pointer to

Why cast “extern puts” to a function pointer “(void(*)(char*))&puts”?

让人想犯罪 __ 提交于 2019-12-12 08:47:56
问题 I'm looking at example abo3.c from Insecure Programming and I'm not grokking the casting in the example below. Could someone enlighten me? int main(int argv,char **argc) { extern system,puts; void (*fn)(char*)=(void(*)(char*))&system; char buf[256]; fn=(void(*)(char*))&puts; strcpy(buf,argc[1]); fn(argc[2]); exit(1); } So - what's with the casting for system and puts? They both return an int so why cast it to void? I'd really appreciate an explanation of the whole program to put it in

Why would one use function pointers to member method in C++?

陌路散爱 提交于 2019-12-12 07:38:47
问题 A lot of C++ books and tutorials explain how to do this, but I haven't seen one that gives a convincing reason to choose to do this. I understand very well why function pointers were necessary in C (e.g., when using some POSIX facilities). However, AFAIK you can't send them a member function because of the "this" parameter. But if you're already using classes and objects, why not just use an object oriented solution like functors? Real world examples of where you had to use such function

Templated Function Pointer in C++

笑着哭i 提交于 2019-12-12 07:06:35
问题 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;

function pointer typedef

纵饮孤独 提交于 2019-12-12 04:35:58
问题 What is the difference between typedef double F(double) and typdedef double (*FPT)(double); ? It seems to me that I can pass both as arguments to a function, i.e. bar1(FPT f); bar2(F f); but while I can do FPT f = &foo; I can not do F f = foo; i.e. I can not create variables of type F? 回答1: You're right in many ways. F is a function type, and FPT is a function pointer type. If you have an object of function type, you can take its address and get a function pointer. However, objects of