Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: is it possible to hold different types of function pointers in a table(structure)

  1. #11
    Join Date
    Aug 2012
    Beans
    590

    Re: is it possible to hold different types of function pointers in a table(structure)

    Quote Originally Posted by nvteighen View Post
    I wouldn't discourage anyone to research on these topics. If he's interested, great and endavant, as Catalans say! However, I agree with you that it's rather counterproductive to try to shoe-horn a homebrewed implementation of dynamic programming into any serious thing you're writing (defining "serious" as "something you care about, be it a hobby or a job"), as you'll be quickly frustrated or may lose a lot of worktime. If you need something like that, you'd either have to switch to a truly dynamic language (I'm excluding you, C++) or, if C is a requirement, use some hard-proven library like GLib or, better, combine C with another language.

    Don't set yourself any limits when learning programming, but be realistic on what your tools can do for you when you need them.
    Thanks nvteighen

  2. #12
    Join Date
    Aug 2012
    Beans
    590

    Smile Re: is it possible to hold different types of function pointers in a table(structure)

    Quote Originally Posted by trent.josephsen View Post
    If the functions have different signatures, you're going to have to do some kind of if-else construct anyway, in order to pass the correct arguments. No form of polymorphism or dynamic typing is possible in C unless you write it yourself.

    You're new to C and trying to do something that is not only unsafe, but needlessly complex and unnecessary. Stop. Learn to use the language properly first; make a text adventure or a program to manage your finances or something. Take a break and learn some assembly instead, if you're into that kind of thing. Then, if some day 10 years from now you find you really need to use dlsym, you won't be simultaneously trying to figure out calling conventions and stack frames and function pointer declarations and so forth.

    It's perfectly possible to write reusable, maintainable code in C, but to do so you'll have to abandon dynamic typing, polymorphism, overloading and other shortcuts that some languages do for you magically. Don't try to force them to work, because even if you succeed, you will only have made an unmaintainable mixture of C and non-C. And there are enough C++ programmers in the world already.
    Sir, okay. Will try for some more time, and if it doesn't happen, won't continue.
    As you said, I should learn things one-by-one, but just that I was so excited to put it all together.
    Thanks once again.

  3. #13
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,921
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: is it possible to hold different types of function pointers in a table(structure)

    Quote Originally Posted by IAMTubby View Post
    "There are 4 functions defined in a program. Assume all 4 functions are of different signature. The program logic executes the correct function depending on the name of the function supplied as input by the user. Needless to say, I don't want to execute the function using an if-else construct like if(user says fun3){fun3();}, but do a dlsym for fun3 and execute using the function pointer returned. "
    Is is possible to achieve this ?

    Thanks.
    If the functions have different signatures then you have to pass parameters differently (and retrieve the returned results differently too(*)....). Then either:
    • you have a finite set of signatures: define a pointer type for each function and keep the pointer in a union (as discussed above)
    • you have random signatures: you have to define some form of description for the signature (C-like or else) that you can keep together with the function pointer, and that you code can use to dynamically construct the call stack structure.


    (*) which is an interesting problem all by itself since C allows returning structures....

  4. #14

    Re: is it possible to hold different types of function pointers in a table(structure)

    Eh, there comes a point where coding for the sake of learning to code, without giving thought to design, becomes counterproductive. I'd argue this is such an area.

    Quote Originally Posted by ofnuts View Post
    define some form of description for the signature (C-like or else) that you can keep together with the function pointer, and that you code can use to dynamically construct the call stack structure.
    I might add (for the benefit of the future reader) that this would be, at best, an imperfect copy of what your C implementation was doing for you to start with. Which, when you realize it, should normally be its own red flag.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •