Understanding pointer casting with const

I am a newbie programming in MSVS2008. I am struggling to understand this casting which is correctly working code:

int compareQuantityAsc(const void *a, const void *b)
{
Album *ia = (Album *)a;
Album *ib = (Album *)b;

Here is what I understand:
1. I understand that the parameters coming into the function are constants -- theyare just numeric values that (in this case) happen to be address numbers pointing to some data in memory. I printf'ed the parameter values and understand what is coming in.
2. I also understand that the left hand side of the equations are declaring new pointers of type "Album".
3. The equal sign is setting the value from the right hand side of the equation to the left-hand side.

What I am not clear of is how to interpret the right side. The best I can make of it is to say "We are declaring a temporary instance pointer of type Album to the location identified through the constant numeric value of a (and b)"

Is this a proper understanding of what is going on on the right-hand side of the equates?

Thanks in advance -
For the code to be const-correct, you should not be casting away the constness at all - to determine the relative ordering between two albums, you do not need to modify the albums at all. General madness will be the result if you modify the objects that are being sorted right in the middle of the sort operation.

1
2
3
4
5
6
int compareQuantityAsc(const void *a, const void *b)
{
      const Album *ia = static_cast<const Album*>(a) ;
      const Album *ib = static_cast<const Album*>(b) ;
      // compare and return result
}


Better still, use the type-safe std::sort() (predicate version), where no casts are required.
http://www.cplusplus.com/reference/algorithm/sort/
Topic archived. No new replies allowed.