From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,a723b6e5f6961330 X-Google-Attributes: gidfee84,public From: Rich Peterson Subject: Re: Bug in CC? (OSF1 4.0) Date: 1997/10/20 Message-ID: <344B9B45.41C6@bigfoot.com>#1/1 X-Deja-AN: 284583680 References: To: Akim Demaille Organization: UBPG Newsgroups: comp.unix.osf.osf1 Akim Demaille wrote: > One of the problems I encounter is a weird behavior of native > cc. On the attached file (cc -E of the file giving problems), I get > the following problems: > > $ cc foo.c -c > cc: Error: foo.c, line 534: In this declaration, the type of > "ds_strccat" is not compatible with the type of a previous declaration > of "ds_strccat" at line number 339 in file foo.c. > ds_strccat (struct dstring *s, char c) > ^ > cc: Error: foo.c, line 624: In this declaration, the type of > "ds_getdelim" is not compatible with the type of a previous > declaration of "ds_getdelim" at line number 353 in file foo.c. > char * > -----^ > > $ uname -a > OSF1 elecsrv.enst.fr V4.0 464 alpha > > The line 534 is > void /* 533 */ > ds_strccat (struct dstring *s, char c) > > and 339 is > > void ds_strccat (); > > Can somebody explain me what is going on? The other is > identical. I have no problem with the other functions! The compiler is correct in issuing a diagnostic for this code. The problem is that one function declaration uses ANSI C prototype syntax to specify the types of the parameters, the other declaration is an old K&R-style declaration, and the prototype-style declaration does not specify parameter types that are compatible with the types that result from the application of the default argument promotions. See section 3.5.4.3 of the ANSI version of the standard (6.5.4.3 of the ISO version). In particular, line 534 says the second parameter to ds_strccat is of type char, while the declaration at line 339 provides no parameter information. Suppose you had only the second declaration present and had the following code: extern void ds_strccat (); extern char x; void foo(void) { ds_strccat(0, x); } The compiler is required to fetch the value of the char variable x, promote it to type int, and pass the value to ds_strccat as an int. If you had only the first declaration: extern void ds_strccat (struct dstring *s, char c); extern char x; void foo(void) { ds_strccat(0, x); } The compiler is required to fetch the value of the char variable x, and pass it to ds_strccat as a char. Compilers are permitted to use different calling conventions to pass a char value versus an int value, so your declarations are specifying two conflicting ways to generate code for calls to the same function. As it happens, the Digital UNIX C compiler does not use very different conventions for char and int parameters, but under certain circumstances you could get somewhat different results (e.g. because the high order bits in the 64-bit register can be left with garbage if the compiler knows that only the low-order 8 bits are used). From the C language point of view, though, the two declarations are just as incompatible as if one said the parameter was float and the other said it was int. If you have lots of other functions that are declared twice, once in prototype-style and the other old-style, and the compiler does not complain about them, then those prototype declarations must be specifying only parameter types that are compatible with the types that result from the default argument promotions (i.e. flavors of int, long, double, pointers, and structs or unions). -Rich