function-pointers

Bubble Sort using pointers to function

戏子无情 提交于 2019-12-08 14:03:21
问题 I'm trying to implement a bubble sort in c by using pointers to function but does not work. Can anyone help me? Here is the code: #include <stdio.h> #include <stdlib.h> void bubbleSort(void** base, size_t length, int (*compar)(const void*, const void*)); int main(int argc, char* argv[]) { int cmp(const void*, const void*); int vet[] = {1, 2, 5, 7, 6, 1, 3, 2, 9, 15, 14, 20}; bubbleSort((void**) &vet, sizeof(vet)/sizeof(vet[0]), cmp); int i; for (i = 0; i < sizeof(vet)/sizeof(vet[0]); i++) {

Generic member function pointer

主宰稳场 提交于 2019-12-08 13:06:33
I'm wondering if there's a way in standard C++ (it seems this is not supported but perhaps I didn't look hard) to declare a pointer to any class' member function with the same signature. For example, X and Y have echoX and echoY methods with the same signature class X{ int val; public: int echoX(int v) { val = v; return v; } int getValue() const { return val; } }; class Y{ int val; public: int echoY(int v) { val = v; return v; } int getValue() const { return val; } }; Some C++ implementations allow this functionality via extensions (e.g VCL makes use of the __closure keyword). typedef int (_

Polymorphism in an array of elements

会有一股神秘感。 提交于 2019-12-08 10:31:54
问题 Suppose I have defined a derived type (in Fortran 2003) named geometry and I extend it to two new derived types: circle and triangle . Each extended type has its own constructor, NewCircle and NewTriangle that returns a circle object and a triangle object respectively. Then I would like to do this: use appropriate_module class(geometry), allocatable :: Geo(:) allocate(Geo(2)) Geo(1) = NewCircle Geo(2) = NewTriangle Of course the last two lines are invalid in Fortran 2003 standard. I do not

what is possible way to use the function from the other file, which was initialized by a static?

最后都变了- 提交于 2019-12-08 07:53:28
My question is simple. I have three files in my program.suppose one of files has static function.how can I use that function from the other one? I knew that static function accessed by the files itself.i want to know any possible way. If the way is available, how can i stop that access in terms of function declaration? A function declared as file-scope static will not be visible from outside the file, meaning that you will not be able to look the function up by name etc. However, if a function from the same file hands out a pointer to the function, code will be able to call it, since there is

what is possible way to use the function from the other file, which was initialized by a static?

烈酒焚心 提交于 2019-12-08 07:34:04
问题 My question is simple. I have three files in my program.suppose one of files has static function.how can I use that function from the other one? I knew that static function accessed by the files itself.i want to know any possible way. If the way is available, how can i stop that access in terms of function declaration? 回答1: A function declared as file-scope static will not be visible from outside the file, meaning that you will not be able to look the function up by name etc. However, if a

How to call pointer to function defined in typedef struct

↘锁芯ラ 提交于 2019-12-08 04:41:16
问题 what is wrong with the following code? parseCounter1() and parseCounter1() below are two functions. I put their pointers in const OptionValueStruct so that they can be called accordingly when each element of option_values[] are gone through: typedef struct OptionValueStruct{ char counter_name[OPTION_LINE_SIZE]; int* counter_func; } OptionValueStruct_t; const OptionValueStruct option_values[] = { {"Counter1", (*parseCounter1)(char*, char**)}, {"Counter2", (*parseCounter2)(char*, char**)}, };

null pointer when getting function pointer using boost::function::target

最后都变了- 提交于 2019-12-08 04:30:00
问题 After reading this answer I thought I had a solution. At least the answer there is what I would like to do but I'm having a problem with the implementation. here is an outline of what I am trying to do typedef map<string, double*> myMap; typedef int (*ftwpt)(const char*, const struct stat*, int); typedef boost::function<int(const char*, const struct stat*, int)> MyFTWFunction; int myFunction(const char*, const struct stat*, int, myMap*); int main() { myMap m_map; char tmpdir[] = "/tmp/mytmp";

Generic member function pointer

萝らか妹 提交于 2019-12-08 04:18:28
问题 I'm wondering if there's a way in standard C++ (it seems this is not supported but perhaps I didn't look hard) to declare a pointer to any class' member function with the same signature. For example, X and Y have echoX and echoY methods with the same signature class X{ int val; public: int echoX(int v) { val = v; return v; } int getValue() const { return val; } }; class Y{ int val; public: int echoY(int v) { val = v; return v; } int getValue() const { return val; } }; Some C++ implementations

C: What even is function pointer conversion? [closed]

十年热恋 提交于 2019-12-08 03:15:39
问题 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 last year . Let's say one wanted to create an array that could hold multiple function pointers & of different types .. how would he go about doing so ? Perhaps an array of void pointers could work ?... Well as it turns out, no since in order to use the functions stored in the void pointers you'd have to convert

calling functions returned as pointers from other functions in ctypes

為{幸葍}努か 提交于 2019-12-07 23:22:27
问题 I'm trying to use ctypes to call a c function that was returned as a pointer from another function. It seems from the documentation that I can do this by declaring the function with CFUNCTYPE , and then creating an instance using the pointer. This, however seems to give me a segfault. Here is some sample code. sample.c: #include <stdio.h> unsigned long long simple(void *ptr) { printf("pointer = %p\n", ptr); return (unsigned long long)ptr; } void *foo() { return (void *)simple; } unsigned long