passing character to function.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kalinga1234@gmail.com

    passing character to function.

    there is a problem regarding passing array of characters to another
    function(withou t using structures,poin ter etc,).can anybody help me to
    solve the problem.

  • Srini

    #2
    Re: passing character to function.

    > there is a problem regarding passing array of characters to another[color=blue]
    > function(withou t using structures,poin ter etc,).can anybody help me to
    > solve the problem.[/color]

    What problem??

    void fun(char arr[]) {
    // ...
    }

    char arr[10];
    // ...
    fun(arr);

    Srini

    Comment

    • John Harrison

      #3
      Re: passing character to function.

      kalinga1234@gma il.com wrote:[color=blue]
      > there is a problem regarding passing array of characters to another
      > function(withou t using structures,poin ter etc,).can anybody help me to
      > solve the problem.
      >[/color]

      Passing arrays is not allowed in C++. You must use pointers or
      references or structs or classes. There is no other way to do it.

      There best way would be to use the C++ string class.

      john

      Comment

      • Greg

        #4
        Re: passing character to function.

        John Harrison wrote:[color=blue]
        > kalinga1234@gma il.com wrote:[color=green]
        > > there is a problem regarding passing array of characters to another
        > > function(withou t using structures,poin ter etc,).can anybody help me to
        > > solve the problem.
        > >[/color]
        >
        > Passing arrays is not allowed in C++. You must use pointers or
        > references or structs or classes. There is no other way to do it.
        >
        > There best way would be to use the C++ string class.
        >
        > john[/color]

        It's legal to pass an array in a function call, it's just not possible
        for the function called to recover it. In C++ an array passed by value
        "decays" into a pointer to its first element by the time the receiver
        gets it.

        One workaround is to pass the array by reference. In fact passing by
        reference is often a better idea than passing it by value, even if it
        were possible to do so:

        void f( int (&inArrayRef)[5]);

        int main()
        {
        int firstArray[5];
        int secondArray[7];

        f( firstArray); // ok
        f( secondArray); // error - wrong size
        }

        Greg

        Comment

        • Jack Klein

          #5
          Re: passing character to function.

          On 16 Sep 2005 05:10:33 -0700, "Greg" <greghe@pacbell .net> wrote in
          comp.lang.c++:
          [color=blue]
          > John Harrison wrote:[color=green]
          > > kalinga1234@gma il.com wrote:[color=darkred]
          > > > there is a problem regarding passing array of characters to another
          > > > function(withou t using structures,poin ter etc,).can anybody help me to
          > > > solve the problem.
          > > >[/color]
          > >
          > > Passing arrays is not allowed in C++. You must use pointers or
          > > references or structs or classes. There is no other way to do it.
          > >
          > > There best way would be to use the C++ string class.
          > >
          > > john[/color]
          >
          > It's legal to pass an array in a function call, it's just not possible[/color]

          No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
          in judgment. The name of an array, in all contexts except when used
          as the operand of the sizeof operator, is converted to a pointer to
          its first element. It is literally impossible to pass a naked array
          by value.
          [color=blue]
          > for the function called to recover it. In C++ an array passed by value
          > "decays" into a pointer to its first element by the time the receiver
          > gets it.[/color]

          It has nothing to do with "when the receiver gets it". The receiver
          receives exactly what the function definition specifies, and when you
          write a definition with a parameter of type TYPE array[], you are
          actually defining a parameter of type TYPE *array. The conversion
          happens in the caller, the array is never passed.

          It is unfortunate that this syntax was ever allowed in a function
          declaration. It has added to the confusion between pointers and
          arrays in C and C++ for more than 30 years. And it leads to erroneous
          statements like "it's legal to pass an array in a function call",
          which help perpetuate the confusion.

          --
          Jack Klein
          Home: http://JK-Technology.Com
          FAQs for
          comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
          comp.lang.c++ http://www.parashift.com/c++-faq-lite/
          alt.comp.lang.l earn.c-c++

          Comment

          • Alf P. Steinbach

            #6
            Re: passing character to function.

            * Jack Klein:[color=blue]
            > * Greg:[color=green]
            > > * John Harrison:[color=darkred]
            > > > * kalinga1234@gma il.com:
            > > > > there is a problem regarding passing array of characters to another
            > > > > function(withou t using structures,poin ter etc,).can anybody help me to
            > > > > solve the problem.
            > > >
            > > > Passing arrays is not allowed in C++. You must use pointers or
            > > > references or structs or classes. There is no other way to do it.
            > > >
            > > > There best way would be to use the C++ string class.[/color]
            > >
            > > It's legal to pass an array in a function call[/color]
            >
            > No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
            > in judgment. The name of an array, in all contexts except when used
            > as the operand of the sizeof operator, is converted to a pointer to
            > its first element.[/color]

            Additional exception: when used as an initializer for an array reference.

            Are there more exceptions?

            Someone else dig into the standard, please; me, I want a cup of coffee now.

            [color=blue]
            > It is literally impossible to pass a naked array by value.[/color]

            In C.

            In C++ we can do it, by passing the array by reference:

            typedef double Triple[3];
            void foo( Triple const& xyz );

            But at the cost of either limiting ourselves to just one size of array, or
            else using the template mechanism (which might result in duplicated code).

            --
            A: Because it messes up the order in which people normally read text.
            Q: Why is it such a bad thing?
            A: Top-posting.
            Q: What is the most annoying thing on usenet and in e-mail?

            Comment

            • Alf P. Steinbach

              #7
              Re: passing character to function.

              * Alf P. Steinbach:[color=blue]
              > * Jack Klein:[color=green]
              > > * Greg:[color=darkred]
              > > > * John Harrison:
              > > > > * kalinga1234@gma il.com:
              > > > > > there is a problem regarding passing array of characters to another
              > > > > > function(withou t using structures,poin ter etc,).can anybody help me to
              > > > > > solve the problem.
              > > > >
              > > > > Passing arrays is not allowed in C++. You must use pointers or
              > > > > references or structs or classes. There is no other way to do it.
              > > > >
              > > > > There best way would be to use the C++ string class.
              > > >
              > > > It's legal to pass an array in a function call[/color]
              > >
              > > No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
              > > in judgment. The name of an array, in all contexts except when used
              > > as the operand of the sizeof operator, is converted to a pointer to
              > > its first element.[/color]
              >
              > Additional exception: when used as an initializer for an array reference.
              >
              > Are there more exceptions?
              >
              > Someone else dig into the standard, please; me, I want a cup of coffee now.
              >
              >[color=green]
              > > It is literally impossible to pass a naked array by value.[/color]
              >
              > In C.
              >
              > In C++ we can do it, by passing the array by reference:
              >
              > typedef double Triple[3];
              > void foo( Triple const& xyz );
              >
              > But at the cost of either limiting ourselves to just one size of array, or
              > else using the template mechanism (which might result in duplicated code).[/color]

              Sorry, I didn't mean that pass-by-reference is pass-by-value: I meant, the
              array is not converted to a pointer in this case, which is what matters.

              --
              A: Because it messes up the order in which people normally read text.
              Q: Why is it such a bad thing?
              A: Top-posting.
              Q: What is the most annoying thing on usenet and in e-mail?

              Comment

              • Kai-Uwe Bux

                #8
                Re: passing character to function.

                Jack Klein wrote:
                [color=blue]
                > On 16 Sep 2005 05:10:33 -0700, "Greg" <greghe@pacbell .net> wrote in
                > comp.lang.c++:
                >[/color]
                [snip][color=blue][color=green]
                >>
                >> It's legal to pass an array in a function call, it's just not possible[/color]
                >
                > No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
                > in judgment. The name of an array, in all contexts except when used
                > as the operand of the sizeof operator, is converted to a pointer to
                > its first element. It is literally impossible to pass a naked array
                > by value.
                >[color=green]
                >> for the function called to recover it. In C++ an array passed by value
                >> "decays" into a pointer to its first element by the time the receiver
                >> gets it.[/color]
                >
                > It has nothing to do with "when the receiver gets it". The receiver
                > receives exactly what the function definition specifies, and when you
                > write a definition with a parameter of type TYPE array[], you are
                > actually defining a parameter of type TYPE *array. The conversion
                > happens in the caller, the array is never passed.
                >
                > It is unfortunate that this syntax was ever allowed in a function
                > declaration. It has added to the confusion between pointers and
                > arrays in C and C++ for more than 30 years. And it leads to erroneous
                > statements like "it's legal to pass an array in a function call",
                > which help perpetuate the confusion.[/color]

                You made me curious.

                Is there an observable difference in what you write and what Greg hinted at?
                For the sake of this discussion, let us suppose there was a language B++
                that is identical to C++ except that it uses Greg's way of passing arrays.
                Does there exist a program that behaves differently when interpreted as C++
                and when interpreted as B++?

                [There is this funny thing that the admissible moves of a knight in chess
                can be given by different rules:

                (a) a knight jumps as though it moved two fields vertically or horizontally
                and then one field in a perpendicular direction.

                (b) a knight jumps as though it moved one field vertically or horizontally
                and then two fields in a perpendicular direction.

                (c) a knight jumps as though it moves one field in a diagonal direction and
                then one field in a horizontal or vertical direction so that it will not
                end on a field neighboring its initial position.

                ....

                On an 8x8 chess board, these rules are provably equivalent. I just wonder if
                you and Greg might conincidentally describe the same thing in different
                words, where yours just happen to be closer to the wording of the
                standard.]


                Best

                Kai-Uwe Bux

                Comment

                • Greg

                  #9
                  Re: passing character to function.

                  Jack Klein wrote:[color=blue]
                  > On 16 Sep 2005 05:10:33 -0700, "Greg" <greghe@pacbell .net> wrote in
                  > comp.lang.c++:
                  >[color=green]
                  > > John Harrison wrote:[color=darkred]
                  > > > kalinga1234@gma il.com wrote:
                  > > > > there is a problem regarding passing array of characters to another
                  > > > > function(withou t using structures,poin ter etc,).can anybody help me to
                  > > > > solve the problem.
                  > > > >
                  > > >
                  > > > Passing arrays is not allowed in C++. You must use pointers or
                  > > > references or structs or classes. There is no other way to do it.
                  > > >
                  > > > There best way would be to use the C++ string class.
                  > > >
                  > > > john[/color]
                  > >
                  > > It's legal to pass an array in a function call, it's just not possible[/color]
                  >
                  > No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
                  > in judgment. The name of an array, in all contexts except when used
                  > as the operand of the sizeof operator, is converted to a pointer to
                  > its first element. It is literally impossible to pass a naked array
                  > by value.
                  >[color=green]
                  > > for the function called to recover it. In C++ an array passed by value
                  > > "decays" into a pointer to its first element by the time the receiver
                  > > gets it.[/color]
                  >
                  > It has nothing to do with "when the receiver gets it". The receiver
                  > receives exactly what the function definition specifies, and when you
                  > write a definition with a parameter of type TYPE array[], you are
                  > actually defining a parameter of type TYPE *array. The conversion
                  > happens in the caller, the array is never passed.
                  >
                  > It is unfortunate that this syntax was ever allowed in a function
                  > declaration. It has added to the confusion between pointers and
                  > arrays in C and C++ for more than 30 years. And it leads to erroneous
                  > statements like "it's legal to pass an array in a function call",
                  > which help perpetuate the confusion.
                  >
                  > --
                  > Jack Klein[/color]

                  Being legal is not the same as being possible.

                  I think it is more confusing to state that passing an array to a
                  function is illegal in C++, even though code that does so compiles
                  without so much as a warning, and executes without a problem. One could
                  easily wonder how many other illegal statements a C++ compiler accepts,
                  and that work as well as this one.

                  It is clearer in my mind simply to state that it is not possible in C++
                  to pass an array intact in a function call. All that such an attempt
                  accomplishes is to pass a pointer. Therefore it is a deficit in the
                  syntax of the language that makes passing an array by value impossible.
                  In light of that fact, deciding whether such an operation - if it could
                  be expressed - would be legal or not, seems a little pointless.

                  Greg

                  Comment

                  Working...