Thread: Confused with const in function parameters

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    45

    Confused with const in function parameters

    My teacher posted sample code with a function with parameters:
    void funcName ( char const * const init )
    What I understand is that both the char and the pointer are constants.

    I'm confused as to what type of pointer init is from the declaration alone.
    Usually if I want to pass a pointer I just do
    void myFunc (int * pointer)
    So the type of the pointer is an int because it's to the left of "*".

    Looking at the code I know that init is really a string.
    So shouldn't the function instead be?:
    void funcName ( char const char* const init)

    Thanks
    Last edited by seal308; 02-19-2016 at 01:08 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Init may be a string. There's other uses for a char pointer. Strings are simply the most common.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by seal308
    My teacher posted sample code with a function with parameters:
    void funcName ( char const * const init )
    What I understand is that both the char and the pointer are constants.
    That is true. However, note that this is only known to be true of the formal parameter; the actual argument could be a non-const pointer to non-const char.

    Quote Originally Posted by seal308
    Usually if I want to pass a pointer I just do
    void myFunc (int * pointer)
    So the type of the pointer is an int because it's to the left of "*".
    No, the type of the pointer is int*, not int. Since the type of the pointer is int*, the type of what the pointer points to is int.

    Quote Originally Posted by seal308
    Looking at the code I know that init is really a char*
    So shouldn't the function instead be?:
    void funcName ( char const char* const init)
    This:
    Code:
    void funcName ( char const* const init)
    is exactly the same as this:
    Code:
    void funcName ( const char* const init)
    except for spelling. That is, when the const qualifier is at the left-most, it applies to what is on its immediate right, whereas it would otherwise apply to what is on its immediate left. Consequently, what you have in mind would be redundant: you would essentially be trying to const qualify the char twice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    Thank you, I understand it perfectly now.
    It clicked when you said that const applies to what is on its immediate left.
    It's easier for me to first think of it as empty of const so:
    Code:
    void funcName (char* init)
    If I want to make the pointer constant I write const with the * on its immediate left.
    Code:
    void funcName (char* const init)
    If I want to make the data constant I write const with the char on its immediate left.
    Code:
    void funcName (char const* init)
    So when reading the parameters, I should read them from right to left.

    Is the advantage of this to prevent memory leaks?


    Quote Originally Posted by laserlight View Post
    That is true. However, note that this is only known to be true of the formal parameter; the actual argument could be a non-const pointer to non-const char.


    No, the type of the pointer is int*, not int. Since the type of the pointer is int*, the type of what the pointer points to is int.


    This:
    Code:
    void funcName ( char const* const init)
    is exactly the same as this:
    Code:
    void funcName ( const char* const init)
    except for spelling. That is, when the const qualifier is at the left-most, it applies to what is on its immediate right, whereas it would otherwise apply to what is on its immediate left. Consequently, what you have in mind would be redundant: you would essentially be trying to const qualify the char twice.
    Last edited by seal308; 02-19-2016 at 02:28 PM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by seal308 View Post
    Is the advantage of this to prevent memory leaks?
    Nope. Const pointers can only protect you from changing some aspect of the pointer - either the address it stores or the underlying value. The variables themselves can still fall out of scope, and if they fall out of scope before you release memory, you've leaked it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2010, 06:50 AM
  2. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  3. const function parameters
    By Tox|k in forum C Programming
    Replies: 4
    Last Post: 05-20-2008, 08:55 AM
  4. const parameters to a function
    By benny5788 in forum C Programming
    Replies: 6
    Last Post: 06-28-2007, 12:45 AM
  5. function overloading. const T and const T* parameters
    By Mario F. in forum C++ Programming
    Replies: 7
    Last Post: 06-07-2006, 11:04 AM

Tags for this Thread