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

R

Ryan Dupuis

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;

}
 
I

Ian Collins

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 );
 
E

Eric Sosman

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

James Kuyper

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

Ryan Dupuis

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 );

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

Seebs

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
 
K

Keith Thompson

Ryan Dupuis said:
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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top