Alright, we all know one oddity about const modifier in pure C - the 'constants' made using it are not really considered as natural. So you cannot declare a regular (not VLA) array using such a constant as a number of items. Although it's annoying, one can live with this, since preprocessor #define may always be used instead. But now to the functions. Let's take the strchr() function from standard library as an example. Let me remind you that this function searches the string for a character passed as the second argument and returns pointer for its first occurrence. The prototype for it is as follows:
Code:
char * strchr(const char *string, int c);
Note that it takes const char * as a first parameter. But what about return value? It is just char *. So is there a problem anyway? Look at the following code snippet:
Code:
const char str[10] = "Hat";
char *ptr;
ptr = strchr(hi, 'H');
*ptr = 'C';
puts(str);
It compiles perfectly. The output is 'Cat', as you might guess. Basically the function produces the same behavior as it was defined like:
Code:
char *strchr(const char *str, int ch)
{
while (*str)
{
if (*str == ch)
return (char *)str;
str++;
}
return NULL;
}
I don't like that. And you? Please share your thoughts on the subject.