incompatible pointer type warning ?

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

    incompatible pointer type warning ?

    Hi all,

    why do I get a message:

    warning: passing arg 1 of `collectInput' from incompatible pointer type

    In 'main' have:

    char inputString[MAX_INPUT_LENGT H];
    collectInput(&i nputString);

    and later on I have:

    void collectInput(ch ar *input){
    scanf("%s", input);
    }

    I know this is simple, but I can't see what's wrong.....

    Regards
    Michael


  • Ian Collins

    #2
    Re: incompatible pointer type warning ?

    Michael wrote:[color=blue]
    > Hi all,
    >
    > why do I get a message:
    >
    > warning: passing arg 1 of `collectInput' from incompatible pointer type
    >
    > In 'main' have:
    >
    > char inputString[MAX_INPUT_LENGT H];
    > collectInput(&i nputString);
    >[/color]
    should be
    collectInput(in putString);

    or
    collectInput(&i nputString[0]);

    &inputString is the address of the array, which in this case is a char**.

    --
    Ian Collins.

    Comment

    • Ratan

      #3
      Re: incompatible pointer type warning ?

      >void collectInput(ch ar *input){[color=blue]
      > scanf("%s", input);[/color]
      Your function accepts a pointer to chanracter...No t a pointer to
      pointer to a character.

      When you declare a variable like "char inputString[MAX_INPUT_LENGT H];"
      inputString[MAX_INPUT_LENGT H]; , inputString itself becomes a pointer
      to character ...So while calling above function you need to pass the
      array name only..
      So[color=blue]
      > collectInput(&i nputString); line need to be modified to " collectInput(in putString);"[/color]

      Michael wrote:[color=blue]
      > Hi all,
      >
      > why do I get a message:
      >
      > warning: passing arg 1 of `collectInput' from incompatible pointer type
      >
      > In 'main' have:
      >
      > char inputString[MAX_INPUT_LENGT H];
      > collectInput(&i nputString);
      >
      > and later on I have:
      >
      > void collectInput(ch ar *input){
      > scanf("%s", input);
      > }
      >
      > I know this is simple, but I can't see what's wrong.....
      >
      > Regards
      > Michael[/color]

      Comment

      • pete

        #4
        Re: incompatible pointer type warning ?

        Michael wrote:[color=blue]
        >
        > Hi all,
        >
        > why do I get a message:
        >
        > warning: passing arg 1 of `collectInput' from incompatible pointer type
        >
        > In 'main' have:
        >
        > char inputString[MAX_INPUT_LENGT H];
        > collectInput(&i nputString);[/color]

        Try:

        collectInput(in putString);

        without the &
        instead.
        [color=blue]
        >
        > and later on I have:
        >
        > void collectInput(ch ar *input){
        > scanf("%s", input);
        > }
        >
        > I know this is simple, but I can't see what's wrong.....[/color]

        The name of an array is automatically converted
        to a pointer to it's first element in most cases,
        such as the case above.

        And make sure that inputString contains a string,
        before passing it as an argument to a function
        that takes a pointer to a string.

        --
        pete

        Comment

        • whyglinux

          #5
          Re: incompatible pointer type warning ?

          Ian Collins wrote:
          [color=blue]
          >
          > &inputString is the address of the array, which in this case is a char**.
          >[/color]

          No, the type of &inputString is not char**, which is a pointer to a
          pointer, but char (*)[MAX_INPUT_LENGT H], which is a pointer to an array.

          Comment

          • SuperKoko

            #6
            Re: incompatible pointer type warning ?


            Ratan wrote:[color=blue][color=green]
            > >void collectInput(ch ar *input){
            > > scanf("%s", input);[/color]
            > Your function accepts a pointer to chanracter...No t a pointer to
            > pointer to a character.
            >
            > When you declare a variable like "char inputString[MAX_INPUT_LENGT H];"
            > inputString[MAX_INPUT_LENGT H]; , inputString itself becomes a pointer
            > to character ...So while calling above function you need to pass the
            > array name only..
            >[/color]
            No.
            inputString doesn't become a pointer to a character (it is the C
            language, not the B language).
            inputString is an *array*.
            &inputString is a pointer to an array (char (*)[MAX_INPUT_LENGT H])
            which is not compatible with a pointer to char (char*).
            array-to-pointer conversions occur very easily, but it arrays are not
            pointers.

            My post is not informative, since whyglinux already pointed that in
            this discussion, but I say that in order to ensure that everybody in
            this discussion will understand that.

            Comment

            • CBFalconer

              #7
              Re: incompatible pointer type warning ?

              Ian Collins wrote:[color=blue]
              > Michael wrote:[color=green]
              >>
              >> why do I get a message:
              >>
              >> warning: passing arg 1 of `collectInput' from incompatible pointer type
              >>
              >> In 'main' have:
              >>
              >> char inputString[MAX_INPUT_LENGT H];
              >> collectInput(&i nputString);[/color]
              >
              > should be
              > collectInput(in putString);
              >
              > or
              > collectInput(&i nputString[0]);
              >
              > &inputString is the address of the array, which in this case is a char**.[/color]

              No, it isn't a char**. While an array reference is often converted
              to a pointer to its first element, that does not make the address
              of an array a pointer to a pointer. Thinking of it as a char**
              leads to gross errors.

              Your actual code corrections are fine.

              --
              "Our enemies are innovative and resourceful, and so are we.
              They never stop thinking about new ways to harm our country
              and our people, and neither do we." -- G. W. Bush.
              "The people can always be brought to the bidding of the
              leaders. All you have to do is tell them they are being
              attacked and denounce the pacifists for lack of patriotism
              and exposing the country to danger. It works the same way
              in any country." --Hermann Goering.


              Comment

              • Keith Thompson

                #8
                Re: incompatible pointer type warning ?

                "SuperKoko" <tabkannaz@yaho o.fr> writes:[color=blue]
                > Ratan wrote:[color=green][color=darkred]
                >> >void collectInput(ch ar *input){
                >> > scanf("%s", input);[/color]
                >> Your function accepts a pointer to chanracter...No t a pointer to
                >> pointer to a character.
                >>
                >> When you declare a variable like "char inputString[MAX_INPUT_LENGT H];"
                >> inputString[MAX_INPUT_LENGT H]; , inputString itself becomes a pointer
                >> to character ...So while calling above function you need to pass the
                >> array name only..
                >>[/color]
                > No.
                > inputString doesn't become a pointer to a character (it is the C
                > language, not the B language).
                > inputString is an *array*.[/color]

                The object called inputString is an array, and of course it doesn't
                become anything.

                The expression intputString, which is the name of an array, is
                implicitly converted to a pointer to its first element in most
                contexts, including this one. So in that sense, it's perfectly
                correct to say that "inputStrin g itself becomes a pointer to
                character".

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • Ian Collins

                  #9
                  Re: incompatible pointer type warning ?

                  CBFalconer wrote:[color=blue]
                  > Ian Collins wrote:
                  >[color=green]
                  >>Michael wrote:
                  >>[color=darkred]
                  >>>why do I get a message:
                  >>>
                  >>>warning: passing arg 1 of `collectInput' from incompatible pointer type
                  >>>
                  >>>In 'main' have:
                  >>>
                  >>>char inputString[MAX_INPUT_LENGT H];
                  >>>collectInput (&inputString );[/color]
                  >>
                  >>should be
                  >>collectInput( inputString);
                  >>
                  >>or
                  >>collectInput( &inputString[0]);
                  >>
                  >>&inputStrin g is the address of the array, which in this case is a char**.[/color]
                  >
                  >
                  > No, it isn't a char**. While an array reference is often converted
                  > to a pointer to its first element, that does not make the address
                  > of an array a pointer to a pointer. Thinking of it as a char**
                  > leads to gross errors.
                  >[/color]
                  Oops, my bad.

                  --
                  Ian Collins.

                  Comment

                  Working...