what's the use of "const char*" as a parameter?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Virtual_X

    #1

    what's the use of "const char*" as a parameter?

    some function make the data type of it's parameters as "const char*"

    why not use char or char* instead
    what would be different

  • Victor Bazarov

    #2
    Re: what's the use of "const char*" as a parameter?

    Virtual_X wrote:
    some function make the data type of it's parameters as "const char*"
    >
    why not use char or char* instead
    what would be different
    The type 'char' can only help passing a _single_ character.

    The type 'char*' can help passing an array of non-constant characters.

    The type 'const char*' helps passing an array of constant characters.

    It means the function needs an array (often it's a C-string), and the
    function promises that it will not change the contents of the array.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Rolf Magnus

      #3
      Re: what's the use of "const char*" as a parameter?

      Victor Bazarov wrote:
      Virtual_X wrote:
      >some function make the data type of it's parameters as "const char*"
      >>
      >why not use char or char* instead
      >what would be different
      >
      The type 'char' can only help passing a _single_ character.
      >
      The type 'char*' can help passing an array of non-constant characters.
      >
      The type 'const char*' helps passing an array of constant characters.
      >
      It means the function needs an array (often it's a C-string),
      Even though it usually does need one for the case of pointers to char, the
      declaration doesn't mean that. The function gets a pointer to a single
      char. It's just a convention between the function and the caller that this
      actually points to the first element of an array.

      Comment

      • subscription.gupta@gmail.com

        #4
        Re: what's the use of "const char*" as a parameter?

        A function which parameter is const char *, it can accept char * and
        const char * type of argument.

        But if function's parameter is only char *, it will give a compilation
        error when you try to
        pass a const char *.


        On Jul 12, 11:30 pm, Rolf Magnus <ramag...@t-online.dewrote:
        Victor Bazarov wrote:
        Virtual_X wrote:
        some function make the data type of it's parameters as "const char*"
        >
        why not use char or char* instead
        what would be different
        >
        The type 'char' can only help passing a _single_ character.
        >
        The type 'char*' can help passing an array of non-constant characters.
        >
        The type 'const char*' helps passing an array of constant characters.
        >
        It means the function needs an array (often it's a C-string),
        >
        Even though it usually does need one for the case of pointers to char, the
        declaration doesn't mean that. The function gets a pointer to a single
        char. It's just a convention between the function and the caller that this
        actually points to the first element of an array.

        Comment

        • Default User

          #5
          Re: what's the use of &quot;const char*&quot; as a parameter? - TPA

          subscription.gu pta@gmail.com wrote:
          A function which parameter is const char *, it can accept char * and
          const char * type of argument.
          Please don't top-post. Your replies belong following or interspersed
          with properly trimmed quotes. See the majority of other posts in the
          newsgroup, or the group FAQ list:
          <http://www.parashift.c om/c++-faq-lite/how-to-post.html>

          Comment

          Working...