Re: strtok_r has prototyped pointer of different width?



Johannes Bauer <dfnsonfsduifb@xxxxxx> wrote in
char x[123];
const char *delim = "abcd";
char *token;
char *saveptr;

strcpy(x, "foobar");
token = strtok_r(x, delim, &saveptr);

Which, compiled with

gcc -O2 -Wall -Wextra -Wformat-nonliteral -Wcast-align -Wpointer-arith
-Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes
-Wmissing-declarations -Winline -Wundef -Wnested-externs -Wcast-qual
-Wshadow -Wconversion -Wwrite-strings -o foo foo.c

Yields (among argc/argv unused) the following warning:

foo.c:13: warning: passing argument 2 of â??__strtok_r_1câ?? with
different width due to prototype

I've found no explanation - why is that?

The option -Wconversion is documented thus:
Warn if a prototype causes a type conversion that is different
from what would happen to the same argument in the absence of a
prototype.

If there were no function prototype, the single char argument to the
internal function __strtok_r_1c would be promoted to an int (generally 4
bytes, but depends on architecture) based on standard C language rules.
With the prototype, the char argument is not promoted. Hence, the width
of the argument passed changed as a result of the prototype, which is
what you asked for with -Wconversion.

Here is the same issue reduced to a minimal example -- compile with "gcc
-Wconversion -c foo.c":

extern void bar(char x);
void foo(void) { char y = 0; bar(y); }

Then comment out the "extern void ..." line and you'll see that the
warning goes away.

GH
.



Relevant Pages

  • Re: comment on my solution to exercise 3.3 in K&Rs The C programming language
    ... foo.c:43: warning: function declaration isn't a prototype ... /* copies over all char, in order to handle literal, ...
    (comp.lang.c)
  • Re: reply the answer
    ... need a prototype for putsbecause it returns int and has no ... const char *, not char *. ... prototype is in scope that says it is okay to pass an argument ... Whether the definition of puts includes a prototype is not ...
    (comp.lang.c)
  • Re: comment on my solution to exercise 3.3 in K&Rs The C programming language
    ... foo.c:43: warning: function declaration isn't a prototype ... /* copies over all char, in order to handle literal, ...
    (comp.lang.c)
  • Re: Can u tell me the explanation reg this problem
    ... value although it's implicitly declared to return int and there isn't a ... proper prototype of printf in scope), ... understood as const char*, ... termination status returned by the program to the operating system ...
    (comp.lang.c)
  • Re: integer promotions
    ... The f's prototype is provided, then an one-byte copy of x is passed to ... No, the value of 'x' is passed as a char, which is one byte by ... The prototype is not provided, then a four-byte int, which contains the ... is passed to f(unix/intel platform). ...
    (comp.lang.c)