djp862@anu.oz.au ("David J Peterson") (06/12/91)
Hello all,
Thanks to all who repsonded on the variable argument problem, I got
that sorted out. Now here's a new one for you.
Is there some reason why I can only pass 4 byte quantities between
function using gcc (v1.39) when I use function prototypes, but can pass
anything I want when I don't use prototypes?
Take a look at the following if that didn't make sense, its a simple
program and the output, or errors from gcc.
--------------------------
#include <stdio.h>
#ifdef PROTO
void printtest(long, int, short, char, char *);
#else
void printtest();
#endif
void main()
{
long l = 4000000000;
int i = 4000000000;
short s = 65000;
char c = 'C';
char *cp = "StringPtr";
printtest(l, i, s, c, cp);
}
void printtest(l, i, s, c, cp)
long l;
int i;
short s;
char c;
char *cp;
{
printf("long\t%d\tsize: %d\n", l, sizeof(l));
printf("int\t%d\tsize: %d\n", i, sizeof(i));
printf("short\t%d\t\tsize: %d\n", s, sizeof(s));
printf("char\t%c\t\tsize: %d\n", c, sizeof(c));
printf("char *\t%s\tsize: %d\n", cp, sizeof(cp));
}
------------------
and then:
------------------
% gcc -o test test.c
% ./test
long -294967296 size: 4
int -294967296 size: 4
short -536 size: 2
char C size: 1
char * StringPtr size: 4
% gcc -DPROTO -o test test.c
test.c
test.c: In function printtest:
test.c:26: argument `s' doesn't match function prototype
test.c:26: a formal parameter type that promotes to `int'
test.c:26: can match only `int' in the prototype
test.c:26: argument `c' doesn't match function prototype
test.c:26: a formal parameter type that promotes to `int'
test.c:26: can match only `int' in the prototype
%
------------------
Whats the deal here? The '-traditional' and/or '-ansi' flags in gcc
don't make any difference. And the native cc doesn't deal with
prototypes at all.
-dave.d88-jwa@byse.nada.kth.se (Jon W{tte) (06/12/91)
> djp862@anu.oz.au ("David J Peterson") writes:
Take a look at the following if that didn't make sense, its a simple
program and the output, or errors from gcc.
void printtest(long, int, short, char, char *);
void printtest(l, i, s, c, cp)
long l;
int i;
short s;
char c;
char *cp;
{
The right and correct way is to change the definition to:
void
printtest ( long l , int i , short s , char c , char * cp )
{
Cheerio,
--
Jon W{tte
h+@nada.kth.se
- Speed !dpassage@soda.berkeley.edu (David G. Paschich) (06/13/91)
In article <D88-JWA.91Jun12173800@byse.nada.kth.se>, d88-jwa@byse.nada.kth.se (Jon W{tte) writes: > djp862@anu.oz.au ("David J Peterson") writes: Take a look at the following if that didn't make sense, its a simple program and the output, or errors from gcc. void printtest(long, int, short, char, char *); void printtest(l, i, s, c, cp) long l; int i; short s; char c; char *cp; { The right and correct way is to change the definition to: void printtest ( long l , int i , short s , char c , char * cp ) { ... the reason being that the function prototype is ansi-style, so the compiler assumes that the arguments will actually be of the indicated size. Then your function definition uses an old-style function definition, in which the compiler assumes that anything smaller than an int will promote to an int. The solution: If you're going to use ansi-style prototypes, then use ansi-style definitions. -- David G. Paschich Open Computing Facility UC Berkeley dpassage@ocf.berkeley.edu "But I'd rather be a fish, 'cause a fish is an animal" -- Gener Fox