Constant pointer to constant value

This is a discussion on Constant pointer to constant value within the C Programming forums, part of the General Programming Boards category; Hello! If you create a function which takes a pointer argument, but never ever changes the pointer or the pointer's ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    32

    Question Constant pointer to constant value

    Hello!

    If you create a function which takes a pointer argument, but never ever changes the pointer or the pointer's value, shouldn't it be declared something like: const char *const myPointer?

    I have never seen anyone use this construct (I googled it and got ZERO useful results), and I'm wondering why. Have I missed something? Is this in some way bad?

    Merry Christmas!
    -tretton

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Having pass-by-value semantics, it doesn't do much to specify the pointer itself as const. It's much like this:
    Code:
    void foo(const int size)
    But declaring what the pointer points to as const does have value and purpose.
    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.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    32
    But it's okay to use double consts? I like to be as strict as possible and when I create functions and don't use certain arguments, it makes sense to me to make them const in every fitting way. Point taken though, about the pass-by-value, but I heard somewhere that declaring stuff as const can make the compiler optimize a little more? (I doubt that any optimization of that magnitude is going to make a difference, but still...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    There's nothing wrong with making things as const as you like.

    So long as you have a more robust approach to design and implementation to avoid lots of random casts to remove const'ness as a quick fix to some mistake earlier on.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Question

    I thought that const was the compilers default type and volatile was the opposite of const though
    am i incorrect

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by hckr83
    I thought that const was the compilers default type and volatile was the opposite of const though
    am i incorrect
    Think of const as "read-only"; think of volatile as "can change through external means (something other than the code I've written)".

    This was a signature of one of the more interesting functions I've written:
    Code:
    static void AudioWriteIso(const volatile char* const buffer, unsigned char size)
    [But looking at it now, there are probably a few things I might change (it's been a few years).]
    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.*

  7. #7
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    There's a nifty program called cdecl that translates ANSI C89 declarations from and to plain english. If the meaning of a declaration is unclear, it is sometimes useful to use this program.
    Insert obnoxious but pithy remark here

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by filker0
    There's a nifty program called cdecl that translates ANSI C89 declarations from and to plain english. If the meaning of a declaration is unclear, it is sometimes useful to use this program.
    Wich is the name of this program?

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    He told you what the name of the program was.
    Quote Originally Posted by Maragato
    Wich is the name of this program?
    Quote Originally Posted by filker0
    There's a nifty program called cdecl that translates ANSI C89 declarations from and to plain english.
    If you understand what you're doing, you're not learning anything.

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    cdecl is a pain in the ... to find in google due to the C/C++ keyword of the same name. Once I downloaded it, I found out that it will choke as soon as a const is involved in the declaration. Nice idea, crappy implementation if you ask me. It's not as if const is some exotic exception.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    And this
    Code:
    const int *pointer;
    is the same as
    Code:
    int const *pointer;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 09:38 AM
  2. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 11:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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