herrcho said:
int intcmp(const void *a, const void *b)
{
return (*(int*)a - *(int*)b);
}
in the above , if i put just 'void' instead of 'const void' as a
parameter,
what's the difference ?
If you're intending to pass this function to qsort or something
that expects a parameter of type int(*)(const void*,const void*)
then you must not change from const void to void, this will be
a constraint violation (wrong type of argument) or else undefined
behaviour (if you say cast it back to the right type).
i can't get the meaning of const when used in parameter..
When used as 'const type *a' it means that you are not allowed to
modify the thing that the pointer is pointing to. Unfortunately
due to the bad way you wrote the function this protection is lost
when you cast the argument to (int*). You can in fact write any
qsort compare function in a completely typesafe manner with no
casts, which is much cleaner code:
int intcmp(const void *va, const void *vb)
{
const int *a = va, *b = vb;
return *a - *b;
}
That way you will get a diagnostic message from the compiler if
you accidentally attempt to modify the contents of the memory.