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* ...

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

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.

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:

Originally Posted by http://code-dynasty.net/articles/C_And_CPP_Programming/80#std::map%3C%3E

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:

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

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:

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.

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.

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.