function prototype declaration

S

Sheldon

Hi,

This is a simple mistake so Iam sure there is someone who can help
with it:

The the file.h:

#define IBFLEN 50000

int IRET, ILEN, IUNIT1, IUNIT2, ILOOP, KERR;

extern void pbbufr_(int *IUNIT1, int *IBUFF, int *IBFLEN, int *ILEN,
int *IRET);

Compiling gives the following error with the function prototype:

error: parse error before numeric constant

I have tried several variations but I am at a lost as the to true
cause. The function is from a FORTRAN library and as such the
arguments must be addresses and not copy. If I change IBFLEN to an int
and not use the #define method, it might work, but I would like to
understand why this error occurs.

Thanks for your help.
/Sheldon
 
J

Joachim Schmitz

Sheldon said:
Hi,

This is a simple mistake so Iam sure there is someone who can help
with it:

The the file.h:

#define IBFLEN 50000
This here
int IRET, ILEN, IUNIT1, IUNIT2, ILOOP, KERR;

extern void pbbufr_(int *IUNIT1, int *IBUFF, int *IBFLEN, int *ILEN,
Conflicts with this. The compiler sees "..., int *50000, ..."
int *IRET);
Compiling gives the following error with the function prototype:

error: parse error before numeric constant

I have tried several variations but I am at a lost as the to true
cause. The function is from a FORTRAN library and as such the
arguments must be addresses and not copy. If I change IBFLEN to an int
and not use the #define method, it might work, but I would like to
understand why this error occurs.

Thanks for your help.
/Sheldon
Bye, Jojo
 
J

Joachim Schmitz

Joachim said:
This here
Addition: don't do this in a header file. Declare them extern here and
define them in a .c file.
Think about what otherwise happens if that header files in #include'd in
other modules of the same program
Conflicts with this. The compiler sees "..., int *50000, ..."
Bye, Jojo
 
C

Chris Dollin

Sheldon said:
Hi,

This is a simple mistake so Iam sure there is someone who can help
with it:

The the file.h:

#define IBFLEN 50000

int IRET, ILEN, IUNIT1, IUNIT2, ILOOP, KERR;

extern void pbbufr_(int *IUNIT1, int *IBUFF, int *IBFLEN, int *ILEN,
int *IRET);

Compiling gives the following error with the function prototype:

error: parse error before numeric constant

The #define says "when you see IBFLEN, see 50000 instead". #defines
don't follow C identifier scope rules; they're just text replacement.

The pbbufr_ [try saying that three times quickly, or even slowly]
declaration becomes

extern void pbbufr_(int *IUNIT1, int *IBUFF, int *50000, int *ILEN,
int *IRET);

which is clearly wrong: argument names can't be integers.

So don't do that. An easy fix is to make your parameter names lower-case,
so they don't clash with the traditional UPPERCASEFORMACROS #define
name. If I were renaming them, I'd also strip of the leading `i` and
disabbreviate the name too -- but your local culture might not take
that much warping.
 
S

Sheldon

This here





Conflicts with this. The compiler sees "..., int *50000, ..."





Bye, Jojo

Hi Jojo,

I see but changing to ... int IBFLEN instead of int *IBFLEN results in
the same error.
Cahnging the argument to: just IBFLEN doesn't help either.
Any suggestions?
/S
 
J

Joachim Schmitz

Sheldon said:
Hi Jojo,

I see but changing to ... int IBFLEN instead of int *IBFLEN results in
the same error.
Indeed, as 'int 50000' is as illegal as 'int *50000'
Cahnging the argument to: just IBFLEN doesn't help either.
Any suggestions?
Drop the variable names, in prototyps the ain't needed. Or make them
lowercase, as Chris Dollin suggested, this would be needed in the function
definition anyway.

Bye, Jojo
 
S

Sheldon

Indeed, as 'int 50000' is as illegal as 'int *50000'


Drop the variable names, in prototyps the ain't needed. Or make them
lowercase, as Chris Dollin suggested, this would be needed in the function
definition anyway.

Bye, Jojo- Dölj citerad text -

- Visa citerad text -

I see. I understand. Made them lower case now and will remove the
names.

Thanks!

/S
 
M

Martin Ambuhl

Sheldon said:
Hi,

This is a simple mistake so Iam sure there is someone who can help
with it:

The the file.h:

#define IBFLEN 50000

int IRET, ILEN, IUNIT1, IUNIT2, ILOOP, KERR;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
All caps is usually used for macro names, not variable names.
extern void pbbufr_(int *IUNIT1, int *IBUFF, int *IBFLEN, int *ILEN,
int *IRET);

Compiling gives the following error with the function prototype:

error: parse error before numeric constant

IBFLEN is not an object, but a value (actually the characters "IBFLEN"
are replaced with the characters "50000" which the compiler then
recognizes as a numeric constant). You can't meaningfully take the
address of numeric constants or dereference them (except in special,
controlled, non-portable situations). That is
&IBFLEN;
and
*IBFLEN;
become after macro substitution
&50000;
and
*50000;
which are not very useful.
 
K

Keith Thompson

Sheldon said:
This here



Conflicts with this. The compiler sees "..., int *50000, ..."
[...]

I see but changing to ... int IBFLEN instead of int *IBFLEN results in
the same error.
Cahnging the argument to: just IBFLEN doesn't help either.
Any suggestions?

You're using the same name, IBFLEN, for a macro and for a function
parameter.

You're also using the same names for several int objects and for
several other parameters: IRET, ILEN, IUNIT1. But several of the
parameters aren't also declared as variables, and vice versa.

It looks like you're trying to write Fortran in C, though I don't know
Fortran well enough to understand the details. In C, function
parameters only need to be declared in the prototype; you don't need
to declare them separately.

Furthermore, variables can be *declared* in headers, but they
shouldn't be *defined* in headers, as you've done here. And if the C
code that uses the header doesn't need to refer to the variables, you
don't need to declare them in the header at all.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top