Format expects argument of type char but argument _ has type int

Discussion in 'C Programming' started by Ryan Dupuis, Oct 1, 2013.

  1. Ryan Dupuis

    Ryan Dupuis Guest

    I've been stuck on this. Now, bare in mind, I am still a beginner. I'm certain there are more efficient ways to code this, but I am writing this foran assignment and just need help resolving my strings from being promoted to integers.

    argh!


    script:
    Script started on Mon 30 Sep 2013 11:18:46 PM EDT

    redo.c: In function ‘main’:

    redo.c:101:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:101:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:102:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:102:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:103:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:103:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:104:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:104:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:105:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:105:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:106:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:106:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:107:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:107:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:108:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:108:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:109:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:109:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:110:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:110:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]

    redo.c:111:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    redo.c:111:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]



    #include <stdio.h>

    #include <math.h>



    int main(void)

    {

    // initialize constants

    const int tot_ctn = 2, tot_ctr_na = 3, tot_ctr_as = 8;



    // initialize variables

    // Contries

    char ct1[256];

    char ct2[256];

    char ct3[256];

    char ct4[256];

    char ct5[256];

    char ct6[256];

    char ct7[256];

    char ct8[256];

    char ct9[256];

    char ct10[256];

    char ct11[256];



    char contt1[256];

    char contt2[256];

    char contt3[256];

    char contt4[256];

    char contt5[256];

    char contt6[256];

    char contt7[256];

    char contt8[256];

    char contt9[256];

    char contt10[256];

    char contt11[256];

    // Medals

    int g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11;

    int s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11;

    int b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11;

    // Totals

    int tot_g, tot_s, tot_b;

    int tot_m_all;

    int tot_ctr;

    int tot_m1, tot_m2, tot_m3, tot_m4, tot_m5, tot_m6, tot_m7, tot_m8, tot_m9,tot_m10, tot_m11;

    // Averages

    int avg_ctr, avg_contt;



    // SCAN TIME!!!



    printf("Use the following format to submit the data:\n");

    printf("Country Continent GoldMedals SilverMedals Bronze Medals\n");

    printf("\n");

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct1[256], &contt1[256], &g1, &s1, &b1);

    printf("Awesome job! Now for country #2:\n");

    scanf("%s %s %d %d %d", &ct2[256], &contt2[256], &g2, &s2, &b2);

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct3[256], &contt3[256], &g3, &s3, &b3);

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct4[256], &contt4[256], &g4, &s4, &b4);

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct5[256], &contt5[256], &g5, &s5, &b5);

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct6[256], &contt6[256], &g6, &s6, &b6);

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct7[256], &contt7[256], &g7, &s7, &b7);

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct8[256], &contt8[256], &g8, &s8, &b8);

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct9[256], &contt9[256], &g9, &s9, &b9);

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct10[256], &contt10[256], &g10, &s10, &b10);

    printf("Let's start with country #1:\n");

    scanf("%s %s %d %d %d", &ct11[256], &contt11[256], &g11, &s11, &b11);

    // SCAN COMPLETE



    // Functions, baby!

    tot_g = g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8 + g9 + g10 + g11;

    tot_s = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11;

    tot_b = b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9 + b10 + b11;

    tot_ctr = tot_ctr_na + tot_ctr_as;

    tot_m1 = g1 + s1 + b1;

    tot_m2 = g2 + s2 + b2;

    tot_m3 = g3 + s3 + b3;

    tot_m4 = g4 + s4 + b4;

    tot_m5 = g5 + s5 + b5;

    tot_m6 = g6 + s6 + b6;

    tot_m7 = g7 + s7 + b7;

    tot_m8 = g8 + s8 + b8;

    tot_m9 = g9 + s9 + b9;

    tot_m10 = g10 + s10 + b10;

    tot_m11 = g11 + s11 + b11;

    tot_m_all = tot_g + tot_s + tot_b;

    // Averages

    avg_ctr = tot_m_all / tot_ctr;

    avg_contt = tot_m_all / tot_ctn;





    //Printing Time!

    printf("COUNTRY\t CONTINENT\t #GOLD\t #SILVER\t #BRONZE\t #TOTAL\n");

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct1[256], contt1[256], g1, s1, b1, tot_m1);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct2[256], contt2[256], g2, s2, b2, tot_m2);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct3[256], contt3[256], g3, s3, b3, tot_m3);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct4[256], contt4[256], g4, s4, b4, tot_m4);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct5[256], contt5[256], g5, s5, b5, tot_m5);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct6[256], contt6[256], g6, s6, b6, tot_m6);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct7[256], contt7[256], g7, s7, b7, tot_m7);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct8[256], contt8[256], g8, s8, b8, tot_m8);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct9[256], contt9[256], g9, s9, b9, tot_m9);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct10[256], contt10[256], g10, s10, b10, tot_m10);

    printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct11[256], contt11[256], g11, s11, b11, tot_m11);

    printf("Totals\t 11\t %d\t %d\t %d\t %d\n", tot_g, tot_s, tot_b, tot_m_all);

    printf("/n");

    printf("The average medals per: Country = %d\t Continent = %d", avg_ctr, avg_contt);



    // END

    return 0;

    }
    Ryan Dupuis, Oct 1, 2013
    #1
    1. Advertising

  2. Ryan Dupuis

    Ian Collins Guest

    Re: Format expects argument of type char but argument _ has typeint

    Ryan Dupuis wrote:

    Please clean up the awful mess that crap google interface makes of you
    posts!

    > I've been stuck on this. Now, bare in mind, I am still a beginner.
    > I'm certain there are more efficient ways to code this, but I am
    > writing this for an assignment and just need help resolving my
    > strings from being promoted to integers.


    > redo.c: In function ‘main’:
    >
    > redo.c:101:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]


    Appears to refer to:

    > scanf("%s %s %d %d %d", &ct1[256], &contt1[256], &g1, &s1, &b1);


    Why are you passing the address of one past the end of the array to
    scanf? You probably wanted to write

    scanf("%s %s %d %d %d", ct1, contt1, &g1, &s1, &b1 );

    --
    Ian Collins
    Ian Collins, Oct 1, 2013
    #2
    1. Advertising

  3. Ryan Dupuis

    Eric Sosman Guest

    Re: Format expects argument of type char but argument _ has typeint

    On 9/30/2013 11:23 PM, Ryan Dupuis wrote:
    > I've been stuck on this. Now, bare in mind, I am still a beginner. I'm certain there are more efficient ways to code this, but I am writing this for an assignment and just need help resolving my strings from being promoted to integers.


    s/bare/bear/. We're all beginners at some point, but Usenet
    is not a good vehicle for full-scale instruction. What you need
    is a textbook, or a tutorial, or a tutor. (You also need to learn
    the fine art of cutting a problem down to size. Did you really
    need to show all twenty-two occurrences of the same mistake? Next
    time, please cut out all that repetition and show a reduced program
    that has the same problem as the original, but without all that
    tiresome and distracting bloat! And what's with the double-spacing?)

    Nonetheless, very briefly:

    > int main(void)
    > {
    > [...]
    > char ct1[256];
    > [...]
    > char contt1[256];
    > [...]
    > scanf("%s %s %d %d %d", &ct1[256], &contt1[256], &g1, &s1, &b1);


    This is wrong, even though the compiler didn't complain
    about it. Each "%s" requires a corresponding `char*', and you've
    provided matching arguments of the right form. Unfortunately,
    they're of the wrong meaning: `&ct1[256]', for instance, is a
    `char*' that points at the 257'th element in the `ct1' array.
    How many elements does `ct1' have? 256, numbered [0] through
    [255]. There *is* no 257'th element (numbered [256]), so you're
    asking scanf() to store the input string starting at ... well,
    someplace that doesn't even exist. This produces what the C
    language rather stuffily terms "undefined behavior," and what
    C programmers term "bug" or "crash" or "@#*!$^!".

    >[...]
    > printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct1[256], contt1[256], g1, s1, b1, tot_m1);


    We don't need all twenty-two misteaks; these two are plenty.
    As with scanf(), each "%s" in a printf() call takes a matching
    `char*'. But this time you didn't provide one: For example, the
    argument matching the first "%s" is `ct1[256]', which is not a
    `char*' but a `char'. For various reason this `char' value gets
    "promoted" to an `int', and that's what the compiler saw was wrong.
    Oh, and by the way: Which of the 256 `char's in `ct1' does it ask
    for? Right again: The non-existent 257'th one, or "@#*!$^!".

    You need to go back to your textbook (or get one) and read
    about arrays, about strings, and about printf() and scanf().
    You appear to have some fundamental misunderstandings there.

    And just to encourage you: Once you've solved your immediate
    problems, I foresee several more potential problems in the code
    you've posted -- but I don't think you're equipped to understand
    what's wrong as yet. Go learn some basics, then try again. And
    try not to be *too* discouraged -- as I said, we're all beginners
    at some point.

    --
    Eric Sosman
    d
    Eric Sosman, Oct 1, 2013
    #3
  4. Ryan Dupuis

    James Kuyper Guest

    Re: Format expects argument of type char but argument _ has typeint

    On 09/30/2013 11:23 PM, Ryan Dupuis wrote:
    > I've been stuck on this. Now, bare in mind, I am still a beginner. I'm certain there are more efficient ways to code this, but I am writing this for an assignment and just need help resolving my strings from being promoted to integers.

    ....
    > char ct1[256];

    ....
    > char contt1[256];

    ....
    > int g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11;
    >
    > int s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11;
    >
    > int b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11;

    ....
    > scanf("%s %s %d %d %d", &ct1[256], &contt1[256], &g1, &s1, &b1);


    The expression ct1[256] refers to the 257th element of the array ct1.
    However, you declared ct1 as having only 256 elements. If there were
    such an element, the expression &ct1[256] would provide a pointer that
    points at that element. In reality, what that expression does is create
    a pointer one past the end of the array. This is legal in itself, such
    pointers are useful in certain idioms. However, such pointers cannot be
    safely dereferenced. You're passed it to scanf() with a format specifier
    of %s. That will cause scanf to attempt to load a string into the memory
    starting at that location, which requires dereferencing the pointer. The
    result is that your code has undefined behavior.

    What you almost certainly want is a pointer to the first element of the
    array, rather than a pointer to one past the end of the array. One way
    to express that is &ct1[0]. However, the designers of C felt that this
    was such a common need that they decided to make it happen
    automatically. If you simply type ct1, it is automatically converted
    into a pointer to the first element. This is both convenient, and a
    common cause of confusion for newbies like yourself.

    The same issue applies to contt1, of course.
    --
    James Kuyper
    James Kuyper, Oct 1, 2013
    #4
  5. Ryan Dupuis

    Ryan Dupuis Guest

    On Monday, September 30, 2013 11:34:48 PM UTC-4, Ian Collins wrote:
    > Ryan Dupuis wrote:
    >
    >
    >
    > Please clean up the awful mess that crap google interface makes of you
    >
    > posts!
    >
    >
    >
    > > I've been stuck on this. Now, bare in mind, I am still a beginner.

    >
    > > I'm certain there are more efficient ways to code this, but I am

    >
    > > writing this for an assignment and just need help resolving my

    >
    > > strings from being promoted to integers.

    >
    >
    >
    > > redo.c: In function ‘main’:

    >
    > >

    >
    > > redo.c:101:1: warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘int’ [-Wformat]

    >
    >
    >
    > Appears to refer to:
    >
    >
    >
    > > scanf("%s %s %d %d %d", &ct1[256], &contt1[256], &g1, &s1, &b1);

    >
    >
    >
    > Why are you passing the address of one past the end of the array to
    >
    > scanf? You probably wanted to write
    >
    >
    >
    > scanf("%s %s %d %d %d", ct1, contt1, &g1, &s1, &b1 );
    >
    >
    >
    > --
    >
    > Ian Collins


    Thanks guys. I've been able to correct the issue.

    If I need help in the future, I'll be sure to reduce the code to only what is relevant to the question.

    Much appreciated.
    Ryan Dupuis, Oct 1, 2013
    #5
  6. Ryan Dupuis

    Seebs Guest

    On 2013-10-01, Ryan Dupuis <> wrote:
    > I've been stuck on this. Now, bare in mind, I am still a beginner. I'm certain there are more efficient ways to code this, but I am writing this for an assignment and just need help resolving my strings from being promoted to integers.


    Okay. This is way larger than it needs to be; you could trim it to a shorter
    program:

    #include <stdio.h>

    int main(void) {
    char buffer[256] = "";
    printf("%s\n", buffer[256]);
    return 0;
    }

    and this would probably give you the same error message.

    > char ct1[256];


    This makes the name "ct1" be an array of 256 values of type char.
    Each of these values can be referenced using a subscript in the
    range 0-255. So:
    First element: ct1[0]
    Last element: ct1[255]

    > scanf("%s %s %d %d %d", &ct1[256], &contt1[256], &g1, &s1, &b1);


    This is almost certainly not what you want, because you're asking it to
    scan into a region of memory starting just after the end of ct1.

    > printf("%s\t %s\t %d\t %d\t %d\t %d\t\n", ct1[256], contt1[256], g1, s1, b1, tot_m1);


    And here's your problem: ct1[256] isn't really valid to begin with here,
    because it's the 257th element of a 256-element array. But even if you used
    a different number, you are requesting a single character out of the array,
    and for historical reasons the character then gets promoted to an int.

    Try just "ct1" here (and also in the scanf calls).

    Basically, what's confusing you here is that you're not catching the
    distinction between the variable's *name* ("ct1") and the variable's
    *declaration* ("char ct1[256]"). So you're trying to use "ct1[256]" as
    though it referred to the whole buffer as a string, but it doesn't; it
    refers to the 257th character. And to the character itself, not its
    location, but %s wants the *address* of the beginning of the string you
    want to print.

    -s
    --
    Copyright 2013, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    Autism Speaks does not speak for me. http://autisticadvocacy.org/
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Oct 1, 2013
    #6
  7. Ryan Dupuis

    Noob Guest

    Re: Format expects argument of type char but argument _ has typeint

    Ryan Dupuis wrote:

    > Now, bare in mind [...]


    Do you mean that we must picture ourselves taking our clothes off?
    Noob, Oct 1, 2013
    #7
  8. Ryan Dupuis <> writes:
    [...]
    > Thanks guys. I've been able to correct the issue.
    >
    > If I need help in the future, I'll be sure to reduce the code to only
    > what is relevant to the question.
    >
    > Much appreciated.


    This site is helpful: http://sscce.org/

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 1, 2013
    #8
  9. Ryan Dupuis

    Eric Sosman Guest

    [OT] Re: Format expects argument of type char but argument _ hastype int

    On 10/1/2013 9:25 AM, Noob wrote:
    > Ryan Dupuis wrote:
    >
    >> Now, bare in mind [...]

    >
    > Do you mean that we must picture ourselves taking our clothes off?


    I tried it, but I'm not as young and beauteous as I used to
    be so all I pictured was a grisly bare.

    --
    Eric Sosman
    d
    Eric Sosman, Oct 2, 2013
    #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. Schnoffos
    Replies:
    2
    Views:
    1,208
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,629
    Old Wolf
    Jan 20, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
  4. Seebs
    Replies:
    9
    Views:
    4,017
    Phil Carmody
    Sep 17, 2009
  5. Jack Bates
    Replies:
    0
    Views:
    229
    Jack Bates
    Sep 9, 2011
Loading...

Share This Page