Functions with const parameters returning pointer issue

This is a discussion on Functions with const parameters returning pointer issue within the C Programming forums, part of the General Programming Boards category; Alright, we all know one oddity about const modifier in pure C - the 'constants' made using it are not ...

  1. #1
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88

    Post Functions with const parameters returning pointer issue

    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.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Yes. It is ugly. It's disgusting. But there wasn't any other way.

    See, you might need strchr for constant strings and you might need it for non-constant strings. In C there is no way to distinguish between them. That is, in C++ you could do something like this, which is impossible in C:
    Code:
    const char *strchr(const char *, char);
    char *strchr(char *, char);
    So to make it work for both cases, they needed to make it as accepting as possible, which they did by accepting a const string as input and not requiring const on the output.
    Now it gives you, as coder, the responsibility to manage whether the output is const or not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Pointer To Functions = Seg Fault
    By misplaced in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2005, 08:03 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

Tags for this Thread


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