Thread: Function pointers syntax and excercise.

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40

    Function pointers syntax and excercise.

    This is from C++ Primer 5th edition excercise 6.54 and 6.55.

    So the first excercise goes like this:
    Write a declaration for a function that takes two int parameters and returns an int, and declare a vector whose elements have this function pointer type.

    I believe I managed to do the first excercise, but I am not sure since the next exercise, which I dont have a clue what to do with, is a followup on the first excercise.

    Code:
    using PF = int(*)(int*, int);
    
        vector<PF> ivec;
    This is how I declared my vector, I have some doubt whether this is correct or not..

    The next exercise:
    Write four functions that add, subtract, multiply, and divide two int values. Store pointers to these values in your vector from the previous exercise.

    Writing the functions are E-Z-P-Z lemon squeezy, but the next part in storing pointers to these values in the vector is killing me.

    I think part of the problem is that I maybe have an incorrect type for the vector. I have tried to just declare pointers to each function, and use each pointer in the vector, but then I think I had to set the type for the vector to int, which I believe is wrong?

    Anyway, any help is much appreciated.
    "Derp, derp, derp" - Me

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Quote Originally Posted by Iceboxes
    So the first excercise goes like this:
    Write a declaration for a function that takes two int parameters and returns an int, and declare a vector whose elements have this function pointer type.

    I believe I managed to do the first excercise, but I am not sure since the next exercise, which I dont have a clue what to do with, is a followup on the first excercise.
    Code:
    using PF = int(*)(int*, int);
     
        vector<PF> ivec;
    This is how I declared my vector, I have some doubt whether this is correct or not..
    Well, why not write a program to check? For example, if what you wrote is correct, I would expect this program to compile and output 5 when run:
    Code:
    #include <iostream>
    #include <vector>
    
    int foo(int a, int b)
    {
        return a + b;
    }
    
    int main()
    {
        using namespace std;
    
        using PF = int(*)(int*, int);
    
        vector<PF> ivec;
    
        ivec.push_back(foo);
        cout << ivec[0](2, 3) << endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your PF is a function-taking-int*-and-int-returning-int, not function-taking-int-and-int-returning-int as you want it to be.

  4. #4
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    Thanks guys! It compiled and I think it is working as it should.

    @laserlight, part of my problem (which I didnt mention) was that I didnt know exactly what to do in order to add the functions to my vector.. Didnt know it was as easy as to use push_back. I tried direct initialization and stuff like that. (Never really compiled, but that is prolly because I just derp at syntax often)

    Anyways, just using push_back to add the functions to the vector, the program is working as it should! Thank you very much!

    @tabstop, MY function is taking int and int... I posted a direct copy of how they did it in the book, but in my program I wrote int and int. >,< So sorry >.>
    "Derp, derp, derp" - Me

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The 'C' syntax for this would be

    typedef int (*PF)(int, int);

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Direct initialization...
    Code:
    int add(int a, int b) { return 0; }
    int sub(int a, int b) { return 0; }
    int mul(int a, int b) { return 0; }
    int mydiv(int a, int b) { return 0; }
    
    int main()
    {
    	using FPtr_t = int(*)(int, int);
    	std::vector<FPtr_t> MyFunc{ &add, &sub, &mul, &mydiv };
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-04-2012, 01:25 AM
  2. Replies: 7
    Last Post: 04-11-2009, 09:44 AM
  3. function syntax
    By R.Stiltskin in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2009, 11:48 AM
  4. Excercise help again
    By gin in forum C++ Programming
    Replies: 17
    Last Post: 07-05-2008, 11:00 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM