Thread: Passing pointer to constants

  1. #1
    Registered User
    Join Date
    Jun 2003

    Passing pointer to constants

    What are some of the uses of passing "pointer to constants"( int const *ci1; ) as parameters to a function? A simple example would be nice. Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    size_t strlen(const char *s);
    The const qualifier means the function strlen should consider *s to be read-only. That is, strlen is not free to modify the contents pointed to by s. The pointer s itself is not const, so it may be changed (you can point to the next char, for example, with s++).
    char *strtok(char *s1, const char *s2);
    On the other hand, strtok, by the absence of the const qualifier, is free to modify the string s1 (which it attempts to do).

    If you had a badstrlen function that might modify the string you give it, would you use that function? Or choose strlen that cannot change your precious string? That's the use of const qualified pointers in the function pararmeter list: it tells the caller that this function cannot change what is being pointed to.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. passing a structure pointer by value
    By Bleech in forum C Programming
    Replies: 6
    Last Post: 07-11-2006, 05:58 PM
  5. Passing a function pointer to a templated type
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 08:31 PM