Pointer question

This is a discussion on Pointer question within the C++ Programming forums, part of the General Programming Boards category; I've just happen seen a pointer code and would like to ask someone to explain for me: Code: typedef void* ...

  1. #1
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200

    Pointer question

    I've just happen seen a pointer code and would like to ask someone to explain for me:
    Code:
    typedef void* (*mFuncPtr) (char *args);
    mFuncPtr mFunc;
    Thanks, i really appreciate.
    Hello, testing testing. Everthing is running perfectly...for now

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    This declares a new type mFuncPtr which is a pointer to a function which looks like "void* foo (char* args)". You have to know that functions have a location in memory; thus, they can be pointed to.

    Code:
    typedef void* (*mFuncPtr) (char* args);
    mFuncPtr mFunc = 0;
    
    void* my_foo (char* args) {
        printf("%s in my_foo", args);
        return 0;
    }
    
    mFunc = my_foo;
    (*mFunc)("I am"); // Ouputs "I am in my_foo"

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by Desolation
    This declares a new type mFuncPtr which is a pointer to a function which looks like "void* foo (char* args)". You have to know that functions have a location in memory; thus, they can be pointed to.

    Code:
    typedef void* (*mFuncPtr) (char* args);
    mFuncPtr mFunc = 0;
    
    void* my_foo (char* args) {
        printf("%s in my_foo", args);
        return 0;
    }
    
    mFunc = my_foo;
    (*mFunc)("I am"); // Ouputs "I am in my_foo"
    or you can call it in simpler form:
    Code:
    mFunc("I am");
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    So it's pretty much like copy the string from "my_foo" and add that string to "mFunc"
    Hello, testing testing. Everthing is running perfectly...for now

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Not at all. The pointer holds the address of the function my_foo. When dereferencing a pointer you get the value to which it points to, right ? Well that value is the my_foo in this case so when I dereference it, I can pass it the arguments required by the function.

    Here's a link pointing (pun intented) to an article I've written on basic STL usage and there is a good example of how function pointers can be used. Enjoy.

    http://code-dynasty.net/articles/C_A...std::map%3C%3E

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    I have that link saved ever since I went to that website for the first time.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Nice ! Don't be afraid to post there as well, we're moving from a forum that's dying but we're looking for new members as well. =]

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    I've got issues with that link.

    Quote Originally Posted by http://code-dynasty.net/articles/C_And_CPP_Programming/80#std::map%3C%3E
    std::vector<>
    This is perhaps the most used container with std::list<>. It basically represents a resizable array. That is, you can use this container just as you would use a regular array, since the [] operator is overloaded. This container is built following the LIFO (Last In First Out) law. This means that you cannot remove an element at the front using the std::vector<> 's built-in functions. You will have to use the function std::remove() to do so but it's not the best solution. Inserting an element at the end of an std::vector<> is done through the push_back() function and removing the last element is as simple as calling pop_back().
    You can add to the front (or middle) of a vector since it supports the insert member function (although adding to the back is certainly more efficient). You can also remove elements from the vector at any position using the erase member function.



    Later on, a nit:
    Quote Originally Posted by http://code-dynasty.net/articles/C_And_CPP_Programming/80#std::map%3C%3E
    Code:
    class IRenderable { 
        public: 
            // This constructor will be called whenever any sub-class 
            // is instanciated and a pointer to this class will be pushed 
            // inside the static vector. 
            IRenderable() { ls.push_back(this); } 
    
            // This destructor simply removes the current class from the vector 
            IRenderable() { erase(ls.begin(), ls.end(), this); } 
    
            virtual void Render() = 0; 
        protected: 
            static std::vector<IRenderable*> ls; 
    };
    The "destructor" is missing the '~'. It might also help to put in the instantiation of the static data member that is missing from that example (to help avoid any confusion).



    Concerning the list container:
    Quote Originally Posted by http://code-dynasty.net/articles/C_And_CPP_Programming/80#std::map%3C%3E
    std::list<>
    The main difference between std::list<> and std::vector<> is that std::list<> has the pop_front() and push_front() member functions. These functions basically do the same job as their counterpart _back(). pop_front() removes the first element and push_front() adds one at the front. Simple as that. Another new function that is different from std::vector<> is erase(). It takes in two parameters: an iterator to the start and an iterator to the end of the selection which we want to erase.
    As mentioned, the vector container also supports the erase member function. Also note that what is discussed concerning the erase member function is only one version of the function which is overloaded to support two different forms, one with a single argument and the other with the two arguments noted (it may be worth at least mentioning).



    Later, concerning maps:
    Code:
    // This is how you declare a map 
    // The unique identifier is the character and 
    // the value is its number of occurence 
    std::map<char, int> c_freq; 
    for(int i = 0; i < 256; i++) 
        // We need to set each counter to 0. 
        c_freq[(char)i] = 0; 
    
    char text[] = "This is a test string, it is not meaningful"; 
    
    // Here we increment the counter of each character 
    // encountered simply by using the key 
    for(int i = 0; text[i] != '\0'; i++) 
        // (text[i]) is the current character in the string 
        // c_freq[(text[i])] returns a reference to the counter of the character 
        c_freq[(text[i])]++;
    The step in red above is unnecessary. You do not need to initialize the values to 0.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    I will make the appropriate changes, I have to disagree with the last bit, unless there is something I am not aware of. I am incrementing the counters but if I don't initialize them, I will get an error saying I am using uninitialized variables. This is unless they are set by default to 0 which I highly doubt because it could break some code like:

    Code:
    class Foo {
        public:
            Foo() { }
            explicit Foo(const Foo&) { }
    };
    
    std::map<int, Foo> myMap; // error, cannot convert from 0 to const Foo

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    In such a case, primitive types are zero initialised, class types are default initialised.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    Very interesting ! Thanks !

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    This means that you cannot remove an element at the front using the std::vector<> 's built-in functions. You will have to use the function std::remove() to do so but it's not the best solution.
    There is a deeper misconception here, methinks. std::remove (and std::remove_if) do not remove from the container, but from a range designated by an iterator pair. In the end, the member erase function must be called to actually remove the 'removed' elements from the container.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21