function-pointers

What happens if I cast a function pointer, changing the number of parameters

若如初见. 提交于 2019-11-27 18:04:05
问题 I'm just beginning to wrap my head around function pointers in C. To understand how casting of function pointers works, I wrote the following program. It basically creates a function pointer to a function that takes one parameter, casts it to a function pointer with three parameters, and calls the function, supplying three parameters. I was curious what would happen: #include <stdio.h> int square(int val){ return val*val; } void printit(void* ptr){ int (*fptr)(int,int,int) = (int (*)(int,int

Functional Programming (Currying) in C / Issue with Types

会有一股神秘感。 提交于 2019-11-27 17:45:52
问题 As a dyed-in-the-wool functional programmer I find it hard not to try to shoehorn my favourite paradigm into whatever language I'm using. While writing some C I found I'd like to curry one of my functions, and then pass around the partially applied function. After reading Is there a way to do currying in C? and heeding the warnings at http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html#Nested-Functions I came up with: #include <stdio.h> typedef int (*function) (int); function g (int a) {

Function pointer as an argument

帅比萌擦擦* 提交于 2019-11-27 17:42:30
Is it possible to pass a function pointer as an argument to a function in C? If so, how would I declare and define a function which takes a function pointer as an argument? Mehrdad Afshari Definitely. void f(void (*a)()) { a(); } void test() { printf("hello world\n"); } int main() { f(&test); return 0; } Michal Sznajder Let say you have function int func(int a, float b); So pointer to it will be int (*func_pointer)(int, float); So than you could use it like this func_pointer = func; (*func_pointer)(1, 1.0); /*below also works*/ func_pointer(1, 1.0); To avoid specifying full pointer type every

Javascript Function-Pointer Assignment

≯℡__Kan透↙ 提交于 2019-11-27 17:35:59
Consider this javascript code: var bar = function () { alert("A"); } var foo = bar; bar = function () { alert("B"); }; foo(); When running this code I get "A". Is this behavior a part of javascript specification and can I rely on it? Yes that is expected and by design. Your question is basically: does foo reference bar as a pointer or reference would in another language? The answer is no: the value of bar at the time of assignment is assigned to foo . siingCoder In other examples, nothing was passed by value; everything was passed by reference. bar and foo are BOTH pointers All vars/handles to

How to declare an __stdcall function pointer

↘锁芯ラ 提交于 2019-11-27 17:09:04
问题 I tried this typedef void (* __stdcall MessageHandler)(const Task*); This compiles but gives me this warning (VS2003): warning C4229: anachronism used : modifiers on data are ignored I want to declare a pointer to a function with stdcall calling convention? What am I doing wrong? 回答1: As MSDN says, the correct way to write this is typedef void (__stdcall *MessageHandler)(const Task*); 来源: https://stackoverflow.com/questions/5298394/how-to-declare-an-stdcall-function-pointer

What does this invocation of a char array cast as a function do?

回眸只為那壹抹淺笑 提交于 2019-11-27 16:17:28
I came across this piece of code: char code[] = "\xb0\x01\x31\xdb\xcd\x80"; int main(int argc, char **argv) { int (*func)(); func = (int (*)()) code; (int)(*func)(); } It is copied from Writing Shellcode for Linux and Windows Tutorial . Could someone explain that what this function invocation (int)(*func)(); is doing? Jean-Baptiste Yunès It calls a function whose machine code is in the array code . The string contains some machine-level instructions ((three I think, have a look at x86 instruction set). func is declared as a pointer to a function that takes no argument and returns an int . func

What are the operations supported by raw pointer and function pointer in C/C++?

让人想犯罪 __ 提交于 2019-11-27 15:17:05
What are all operations supported by function pointer differs from raw pointer? Is > , < , <= , >=operators supported by raw pointers if so what is the use? For both function and object pointers, they compile but their result is only guaranteed to be consistent for addresses to sub-objects of the same complete object (you may compare the addresses of two members of a class or array) and if you compare a function or object against itself. Using std::less<> , std::greater<> and so on will work with any pointer type, and will give consistent results, even if the result of the respective built-in

Function Pointer - Automatic Dereferencing [duplicate]

China☆狼群 提交于 2019-11-27 13:37:50
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How does dereferencing of a function pointer happen? void myprint(char* x) { printf("%s\n", x); } int main() { char* s = "hello"; void (*test)(char*); void (*test2)(char*); test = myprint; test2 = &myprint; test(s); (*test)(s); test2(s); (*test2)(s); } Can anyone explain to me why all of the above code is valid? "hello" is printed four times. By applying the function pointer, is it implicitly derefenced?

How to read so many stars and parentheses in a templated function-pointer declaration? [duplicate]

隐身守侯 提交于 2019-11-27 13:13:26
问题 This question already has answers here : Complex C declaration (7 answers) Closed last year . From Introduction to the C++11 feature: trailing return types The article claims template <class T> class tmp { public: int i; }; auto foo()->auto(*)()->tmp<int>(*)(){ return 0; } is equivalent to template <class T> class tmp{ public: int i; }; tmp<int> (*(*foo())())() { return 0; } I don't understand the complex function in the second code example. Where should I look at in the beginning? I guess is

Convert C++ function pointer to c function pointer

我是研究僧i 提交于 2019-11-27 13:12:37
I am developing a C++ application using a C library. I have to send a pointer to function to the C library. This is my class: class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); private: Ui::MainWindow *ui; void f(int*); private slots: void on_btn_clicked(); }; This is my on_btn_clicked function: void MainWindow::on_btn_clicked() { void (MainWindow::* ptfptr) (int*) = &MainWindow::f; c_library_function(static_cast<void()(int*)>(ptfptr), NULL); } The C function should get a pointer to a such function : void f(int*). But the code above doesn't work,