Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' |
| Date | Sat, 07 Nov 2015 13:42:35 -0800 |
| Organization | None to speak of |
| Lines | 56 |
| Message-ID | <ln7flt1o6s.fsf@kst-u.example.com> (permalink) |
| References | <bce8216e-9802-4629-a3f3-2e91a2e616c5@googlegroups.com> <563E2C6B.2030302@verizon.net> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="27638"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+VMNQBOgMnQc5pTnFJ4I7w" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:v2BVKLk44D0zF+gJmkLHhElk2eQ= sha1:tNIifjk5KS97kMzYaB8pDbAv3WQ= |
| Xref | csiph.com comp.lang.c:75296 |
Show key headers only | View raw
James Kuyper <jameskuyper@verizon.net> writes:
> On 11/07/2015 11:24 AM, Alla _ wrote:
>> Here is a simple program from the book. When I compile it, the compiler
>> generates a warning addressing it to the part with scanf():
>>
>> warning: format '%i' expects type 'int *', but argument 2 has type
>> 'enum month *'
[...]
>> enum month {January = 1, February, March, April, May, June, July, August, September, October, November, December};
>> enum month a_month;
>>
>> int days;
>>
>> printf("Enter month number: ");
>> scanf("%i", &a_month);
[...]
>> Obviously, authors of the book didn't get that warning, or ignored it.
>> I understand that I am asking for an integer, the number of a month, hence
>> type int %i; but asking scanf to put that value into enum month. I know
>> that enumeration values (names in curly braces) are treated by compiler
>> as integers. But the variable a_month has the 'enum month' type.
>
> Correct. "Each enumerated type shall be compatible with char, a signed
> integer type, or an unsigned integer type. The choice of type is
> implementation-defined,128) but shall be capable of representing the
> values of all the members of the enumeration." (6.7.2.2p4). Note, in
> particular, that "enum month" need not be compatible with 'int'. The
> compatible type could easily be "unsigned char" in this case.
>
>> Is there a way to avoid such warning without altering the program's
>> simplicity?
>
> Yes. Declare a_month to have the type 'int' rather than "enum month".
Or you can read into a temporary int variable, and copy its value to
a_month:
int month_number;
int result = scanf("%d", &month_number);
if (result == 1) {
a_month = month_number;
}
else {
/* error handing */
}
Note that I've changed the format from "%i" (which accepts decimal,
octal, or hexadecimal input) to "%d" (which accepts decimal input). If
the user enters "09", presumably you want to treat it as 9, not as an
error.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' Alla _ <modelling.data@gmail.com> - 2015-11-07 08:24 -0800
Re: Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' James Kuyper <jameskuyper@verizon.net> - 2015-11-07 11:52 -0500
Re: Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' Keith Thompson <kst-u@mib.org> - 2015-11-07 13:42 -0800
Re: Warning: format '%i' expects type 'int *', but argument 2 has type 'enum month *' Barry Schwarz <schwarzb@dqel.com> - 2015-11-07 16:45 -0800
csiph-web