0

In C, if we have a point to an integer, we can write *ptr=4 (assume malloc done).

Suppose now to have a pointer to a function, name it fPtr.

By following the same way as the example before, is it possible to write something like "*fPtr=new_function_typed_here_not_defined_elsewhere"?

In case of not, may I ask if would it be possible by using C++? (which I do not know now, but it could be a nice motivation for starting learning).

(This is an edited version of a question that I originally made)

dMuk
  • 1
  • 1
  • What do you mean by "functions are created"? Because in C it's not possible to "create" functions at run-time. Perhaps all you really need is *one* function, which receives `N` as an argument, does some calculation involving `N`, and stores the result in an *array* of `N` elements? – Some programmer dude Oct 09 '17 at 17:15
  • How are you going to create functions runtime? It is not an interpreted language. And what is the shift function? – 0___________ Oct 09 '17 at 17:15
  • This is unclear to me. Seems you just want `int f(int x, int k) {return x << k;}` or ? – Support Ukraine Oct 09 '17 at 17:16
  • It is impossible to do this while fully respecting standard C. – Max Oct 09 '17 at 17:17
  • Also, please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 09 '17 at 17:18
  • My apologize - I'll edit my question soon :) – dMuk Oct 09 '17 at 17:18
  • In C++ you can create as many instances of the class as you with and have the reaources – 0___________ Oct 09 '17 at 17:18
  • Unfortunately I do not know C++. I am planning to learn it, and to know that a problem like that is easier to solve gives surely a good motivation :) – dMuk Oct 09 '17 at 17:28
  • Sorry but this question is unclear - I'll have to vote to close. – Support Ukraine Oct 09 '17 at 17:31
  • I'll edit soon, no problem. – dMuk Oct 09 '17 at 17:35
  • 2
    To answer your edit: in C, functions are not first-class citizen. What you ask for is a *lambda expression* - an unnamed function literal, the way `"is a string"` is a string (null-terminated char array) literal and `42` is an int literal. C does not have those. You'd have to upgrade to modern C++ for this purpose. –  Oct 09 '17 at 17:38
  • That sounds like lambda functions - that's not supported in c – Support Ukraine Oct 09 '17 at 17:39
  • @Arkadiy great, that's *exactly* what I was searching for! Unfortunately since I do not have a background in Computer Science, I was unaware of the concept of "lambda calculus". It is very nice to know how C++ supports it - it seems it's the right time for learning it. Thanks again for your time and your help. – dMuk Oct 09 '17 at 17:42
  • @Arkadiy It's not part of the standard, but there *is* an extension in clang which allows blocks, which are basically lambda functions, in C. It started out as an Apple-specific extension, but I think it's available on other platforms through clang if you compile using the `-fblocks` flag (you might need to link against a special library as well). On macOS and iOS it'll just work. – Charles Srstka Oct 09 '17 at 17:43
  • @dMuk, well, the modern C++ supports it better than C.You may want to look into this book: "Structure and Interpretation of Computer Programs" –  Oct 09 '17 at 17:44
  • @Arkadiy you're right, I guess I wrote a duplicate question unintentionally. I will have surely a look for the book you suggested. In order to be prepared, may I ask for an example, now? I mean: suppose I have a "function pointer" in C++, and I want to initialize it as the constant function that gives 1 for every input. What will be the right syntax? (I think an example will surely help me when, in the future, I come back to this original problem after having read the appropriate books). – dMuk Oct 09 '17 at 17:52
  • `std::function createIncrementer(int n) { return [n](int x){return x + n;};}` –  Oct 09 '17 at 18:01
  • @Arkadiy copied and pasted in my notes - thanks so much! Again, I would like to express my apologize for the duplicated question, and my gratitude for your help. Best, dMuck – dMuk Oct 09 '17 at 18:03
  • @dMuk, note that I change the comment - the resulting function should be int(int) - take int and return int. –  Oct 09 '17 at 18:04
  • @Arkadiy perfect, thanks for the remark. – dMuk Oct 09 '17 at 18:11

0 Answers0