Passing pointer to constants

This is a discussion on Passing pointer to constants within the C Programming forums, part of the General Programming Boards category; What are some of the uses of passing "pointer to constants"( int const *ci1; ) as parameters to a function? ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    28

    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
    Posts
    5,006
    Code:
    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++).
    Code:
    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, 05:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  4. passing a structure pointer by value
    By Bleech in forum C Programming
    Replies: 6
    Last Post: 07-11-2006, 06:58 PM
  5. Passing a function pointer to a templated type
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 09:31 PM

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