going crazy, invalid conversion from `char*' to `char' help needed

Discussion in 'C Programming' started by spoilsport, Mar 3, 2004.

  1. spoilsport

    spoilsport Guest

    Ive got to write a multi-function program and I'm plugging in the
    functions but I keep getting this error from line 40. Im new to this
    and cant find an answer anywhere.

    Sam


    #include <stdio.h>

    int main (void)
    {
    void top_of_page_info (void);
    void print_item_info (char item_name, double item_price);

    char item_name[15], name;
    double item_price;

    printf ("\n\nPlease enter the name of the item: ");
    scanf ("%s", & item_name);

    printf ("\n\nPease enter the price of the item: $");
    scanf ("%f", & item_price);

    top_of_page_info ();
    print_item_info (item_name, item_price); /***LINE 40***/

    return 0;

    }

    /*******BEGIN TOP_OF_PAGE_INFO
    FUNCTION**************************************/

    void top_of_page_info (void)
    {
    printf ("\n\n PROGRAM DESCRIPTION\n");
    printf (" by\n");
    printf (" YOUR NAME HERE\n");
    printf (" Date Written: DDMMMYY\n");
    printf ("------------------------------------------------------\n\n");
    }

    /*******BEGIN PRINT_ITEM_INFO
    FUNCTION**************************************/

    void print_item_info (char item_name, double item_price)

    {
    printf ("\n\nThe item name is %s\n", item_name);
    printf ("The item price is %f\n", item_price);
    }

    /*************************************************************************************************/
    spoilsport, Mar 3, 2004
    #1
    1. Advertising

  2. spoilsport

    Leor Zolman Guest

    On 2 Mar 2004 19:17:45 -0800, (spoilsport)
    wrote:

    >Ive got to write a multi-function program and I'm plugging in the
    >functions but I keep getting this error from line 40. Im new to this
    >and cant find an answer anywhere.


    I guess you are...I haven't seen a compiler that doesn't give a clear,
    concise message for this type of error. Here's what MSVC 7.1 says:

    sam.c(18) : warning C4047: 'function' : 'char' differs in levels of
    indirection from 'char [15]'

    If this is driving you crazy, you're going to have quite a challenge in
    front of you...

    >
    >Sam
    >
    >
    >#include <stdio.h>
    >
    >int main (void)
    >{
    > void top_of_page_info (void);
    > void print_item_info (char item_name, double item_price);


    item_name is a "string", represented in C as an array of char or a pointer
    to char (an as a function parameter, both definitions say the same thing:
    that it is a pointer to char). So, picking one:

    void print_item_info(const char *item_name, double item_price);

    The const is to advertise that the text won't be changed as a result of
    calling the function.

    >
    > char item_name[15], name;
    > double item_price;
    >
    > printf ("\n\nPlease enter the name of the item: ");
    > scanf ("%s", & item_name);


    Don't need the & there; a naked array name used in this context is
    equivalent to taking the address of its first element, so
    scanf("%s", item_name);
    and
    scanf("%s", &item_name[0]);
    are both okay (the former being easier to type and the way most folks do
    it). The way you did it tends to be allowed by compilers, but I'd use just
    the array name.

    Now, scanf has lots of problems: you can't have white space in the item
    name, if you enter too many characters it'll overrun the buffer...look
    into, say, fgets and sscanf together as an alternative to using scanf.

    >
    > printf ("\n\nPease enter the price of the item: $");
    > scanf ("%f", & item_price);
    >
    > top_of_page_info ();
    > print_item_info (item_name, item_price); /***LINE 40***/


    This should now work with the fixed declaration/definition of
    print_item_info.

    >
    >return 0;
    >
    >}
    >
    >/*******BEGIN TOP_OF_PAGE_INFO
    >FUNCTION**************************************/
    >
    >void top_of_page_info (void)
    >{
    > printf ("\n\n PROGRAM DESCRIPTION\n");
    > printf (" by\n");
    > printf (" YOUR NAME HERE\n");
    > printf (" Date Written: DDMMMYY\n");
    > printf ("------------------------------------------------------\n\n");
    >}
    >
    >/*******BEGIN PRINT_ITEM_INFO
    >FUNCTION**************************************/
    >
    >void print_item_info (char item_name, double item_price)

    void print_item_info(const char *item_name, double item_price)

    >
    >{
    > printf ("\n\nThe item name is %s\n", item_name);
    > printf ("The item price is %f\n", item_price);
    >}
    >
    >/*************************************************************************************************/



    BTW, nicely formatted program! It is a real treat to see early efforts
    posted so legibly...kudos!

    Good luck,
    -leor

    Leor Zolman
    BD Software

    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, Mar 3, 2004
    #2
    1. Advertising

  3. spoilsport

    Jack Klein Guest

    On 2 Mar 2004 19:17:45 -0800, (spoilsport)
    wrote in comp.lang.c:

    In addition to all of Leor's comments:

    > Ive got to write a multi-function program and I'm plugging in the
    > functions but I keep getting this error from line 40. Im new to this
    > and cant find an answer anywhere.
    >
    > Sam
    >
    >
    > #include <stdio.h>
    >
    > int main (void)
    > {
    > void top_of_page_info (void);
    > void print_item_info (char item_name, double item_price);


    Putting function prototypes inside of a function is not illegal, but
    it is not good practice.

    > char item_name[15], name;
    > double item_price;
    >
    > printf ("\n\nPlease enter the name of the item: ");
    > scanf ("%s", & item_name);
    >
    > printf ("\n\nPease enter the price of the item: $");
    > scanf ("%f", & item_price);


    You need to use the "%lf" conversion specifier with any of the
    *scanf() functions for a double. "%f" is for float.

    --
    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.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Mar 3, 2004
    #3
  4. On 2 Mar 2004 19:17:45 -0800, (spoilsport)
    wrote:

    >Ive got to write a multi-function program and I'm plugging in the
    >functions but I keep getting this error from line 40. Im new to this
    >and cant find an answer anywhere.
    >
    >Sam
    >
    >
    >#include <stdio.h>
    >
    >int main (void)
    >{
    > void top_of_page_info (void);
    > void print_item_info (char item_name, double item_price);


    You apparently expect the compiler to recognize that item_name is an
    array and therefore intended the first parameter to be an array of
    char. It doesn't work that way. In a prototype, the names of the
    parameters are irrelevant. The only thing that counts is the
    parameter type. In this case, the first parameter is a char.

    >
    > char item_name[15], name;
    > double item_price;
    >
    > printf ("\n\nPlease enter the name of the item: ");
    > scanf ("%s", & item_name);
    >
    > printf ("\n\nPease enter the price of the item: $");
    > scanf ("%f", & item_price);
    >
    > top_of_page_info ();
    > print_item_info (item_name, item_price); /***LINE 40***/


    Here you call the function but the first argument is an array of char.
    Your compiler is correctly telling you that the first argument is
    incompatible with the expected type. That is, there is no way for the
    compiler to automatically convert the array to a char.

    If you replace the parameter in the prototype with either
    char[]
    or
    char item_name[]
    (the 15 is optional and irrelevant in either case), your argument will
    match the type of the parameter and the compiler will not complain.

    >
    >return 0;
    >
    >}
    >
    >/*******BEGIN TOP_OF_PAGE_INFO
    >FUNCTION**************************************/
    >
    >void top_of_page_info (void)
    >{
    > printf ("\n\n PROGRAM DESCRIPTION\n");
    > printf (" by\n");
    > printf (" YOUR NAME HERE\n");
    > printf (" Date Written: DDMMMYY\n");
    > printf ("------------------------------------------------------\n\n");
    >}
    >
    >/*******BEGIN PRINT_ITEM_INFO
    >FUNCTION**************************************/
    >
    >void print_item_info (char item_name, double item_price)


    You must also change the parameter in the function definition.

    >
    >{
    > printf ("\n\nThe item name is %s\n", item_name);


    Obviously, you really wanted to receive an array and not just a single
    char.

    > printf ("The item price is %f\n", item_price);


    Use %lf for doubles.

    >}
    >
    >/*************************************************************************************************/




    <<Remove the del for email>>
    Barry Schwarz, Mar 4, 2004
    #4
  5. spoilsport

    Leor Zolman Guest

    On 4 Mar 2004 02:42:35 GMT, Barry Schwarz <> wrote:

    >
    >> printf ("The item price is %f\n", item_price);

    >
    >Use %lf for doubles.


    I think it is the scanf that needs the %lf, not the printf.
    I didn't even catch that error in the scanf call, Jack did (good eyes,
    Jack).
    -leor

    >
    >>}
    >>
    >>/*************************************************************************************************/

    >
    >
    >
    ><<Remove the del for email>>


    Leor Zolman
    BD Software

    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, Mar 4, 2004
    #5
  6. spoilsport

    BartenderSam Guest

    FYI, thanks for the help fellows, I was able to plub the other 3 functions
    in and it ran like a champ.

    ---
    Sam
    BartenderSam, Mar 4, 2004
    #6
    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. Navin
    Replies:
    3
    Views:
    650
    Navin
    Jul 28, 2003
  2. Neil Monk
    Replies:
    2
    Views:
    494
    Neil Monk
    Jul 20, 2004
  3. Tim Johansson
    Replies:
    15
    Views:
    8,366
    Christopher Benson-Manica
    Apr 8, 2004
  4. Jonas Cord
    Replies:
    2
    Views:
    2,097
    Jonas Cord
    May 23, 2005
  5. Replies:
    0
    Views:
    820
Loading...

Share This Page