incompatible pointer type warning ?

Discussion in 'C Programming' started by Michael, Jun 5, 2006.

  1. Michael

    Michael Guest

    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_LENGTH];
    collectInput(&inputString);

    and later on I have:

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

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

    Regards
    Michael
     
    Michael, Jun 5, 2006
    #1
    1. Advertising

  2. Michael

    Ian Collins Guest

    Michael wrote:
    > 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_LENGTH];
    > collectInput(&inputString);
    >

    should be
    collectInput(inputString);

    or
    collectInput(&inputString[0]);

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

    --
    Ian Collins.
     
    Ian Collins, Jun 5, 2006
    #2
    1. Advertising

  3. Michael

    Ratan Guest

    >void collectInput(char *input){
    > scanf("%s", input);

    Your function accepts a pointer to chanracter...Not a pointer to
    pointer to a character.

    When you declare a variable like "char inputString[MAX_INPUT_LENGTH];"
    inputString[MAX_INPUT_LENGTH]; , inputString itself becomes a pointer
    to character ...So while calling above function you need to pass the
    array name only..
    So
    > collectInput(&inputString); line need to be modified to " collectInput(inputString);"


    Michael wrote:
    > 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_LENGTH];
    > collectInput(&inputString);
    >
    > and later on I have:
    >
    > void collectInput(char *input){
    > scanf("%s", input);
    > }
    >
    > I know this is simple, but I can't see what's wrong.....
    >
    > Regards
    > Michael
     
    Ratan, Jun 5, 2006
    #3
  4. Michael

    pete Guest

    Michael wrote:
    >
    > 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_LENGTH];
    > collectInput(&inputString);


    Try:

    collectInput(inputString);

    without the &
    instead.

    >
    > and later on I have:
    >
    > void collectInput(char *input){
    > scanf("%s", input);
    > }
    >
    > I know this is simple, but I can't see what's wrong.....


    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
     
    pete, Jun 5, 2006
    #4
  5. Michael

    whyglinux Guest

    Ian Collins wrote:

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


    No, the type of &inputString is not char**, which is a pointer to a
    pointer, but char (*)[MAX_INPUT_LENGTH], which is a pointer to an array.
     
    whyglinux, Jun 5, 2006
    #5
  6. Michael

    SuperKoko Guest

    Ratan wrote:
    > >void collectInput(char *input){
    > > scanf("%s", input);

    > Your function accepts a pointer to chanracter...Not a pointer to
    > pointer to a character.
    >
    > When you declare a variable like "char inputString[MAX_INPUT_LENGTH];"
    > inputString[MAX_INPUT_LENGTH]; , inputString itself becomes a pointer
    > to character ...So while calling above function you need to pass the
    > array name only..
    >

    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_LENGTH])
    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.
     
    SuperKoko, Jun 5, 2006
    #6
  7. Michael

    CBFalconer Guest

    Ian Collins wrote:
    > Michael wrote:
    >>
    >> why do I get a message:
    >>
    >> warning: passing arg 1 of `collectInput' from incompatible pointer type
    >>
    >> In 'main' have:
    >>
    >> char inputString[MAX_INPUT_LENGTH];
    >> collectInput(&inputString);

    >
    > should be
    > collectInput(inputString);
    >
    > or
    > collectInput(&inputString[0]);
    >
    > &inputString is the address of the array, which in this case is a char**.


    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.
     
    CBFalconer, Jun 5, 2006
    #7
  8. "SuperKoko" <> writes:
    > Ratan wrote:
    >> >void collectInput(char *input){
    >> > scanf("%s", input);

    >> Your function accepts a pointer to chanracter...Not a pointer to
    >> pointer to a character.
    >>
    >> When you declare a variable like "char inputString[MAX_INPUT_LENGTH];"
    >> inputString[MAX_INPUT_LENGTH]; , inputString itself becomes a pointer
    >> to character ...So while calling above function you need to pass the
    >> array name only..
    >>

    > No.
    > inputString doesn't become a pointer to a character (it is the C
    > language, not the B language).
    > inputString is an *array*.


    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 "inputString itself becomes a pointer to
    character".

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jun 5, 2006
    #8
  9. Michael

    Ian Collins Guest

    CBFalconer wrote:
    > Ian Collins wrote:
    >
    >>Michael wrote:
    >>
    >>>why do I get a message:
    >>>
    >>>warning: passing arg 1 of `collectInput' from incompatible pointer type
    >>>
    >>>In 'main' have:
    >>>
    >>>char inputString[MAX_INPUT_LENGTH];
    >>>collectInput(&inputString);

    >>
    >>should be
    >>collectInput(inputString);
    >>
    >>or
    >>collectInput(&inputString[0]);
    >>
    >>&inputString is the address of the array, which in this case is a char**.

    >
    >
    > 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.
    >

    Oops, my bad.

    --
    Ian Collins.
     
    Ian Collins, Jun 5, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    1,183
    Ian Collins
    Jul 26, 2006
  2. Replies:
    0
    Views:
    1,050
  3. Replies:
    1
    Views:
    513
    Andrew Poelstra
    Jul 26, 2006
  4. PraZ
    Replies:
    6
    Views:
    902
    Gernot Frisch
    Mar 30, 2006
  5. DFouton
    Replies:
    1
    Views:
    1,020
    DFouton
    Nov 14, 2010
Loading...

Share This Page