Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   function prototype declaration (http://www.velocityreviews.com/forums/t594820-function-prototype-declaration.html)

Sheldon 02-28-2008 09:39 AM

function prototype declaration
 
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

Joachim Schmitz 02-28-2008 09:44 AM

Re: function prototype declaration
 
Sheldon wrote:
> 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



Joachim Schmitz 02-28-2008 09:48 AM

Re: function prototype declaration
 
Joachim Schmitz wrote:
> Sheldon wrote:
>> 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;

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

>>
>> 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



Chris Dollin 02-28-2008 09:49 AM

Re: function prototype declaration
 
Sheldon wrote:

> 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.

--
"Ashes are burning the way." - Renaissance, /Ashes Are Burning/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


Sheldon 02-28-2008 10:02 AM

Re: function prototype declaration
 
On 28 Feb, 10:44, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
> Sheldon wrote:
> > 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


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

Joachim Schmitz 02-28-2008 10:08 AM

Re: function prototype declaration
 
Sheldon wrote:
> On 28 Feb, 10:44, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
> wrote:
>> Sheldon wrote:
>>> 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

>
> 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



Sheldon 02-28-2008 10:16 AM

Re: function prototype declaration
 
On 28 Feb, 11:08, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
> Sheldon wrote:
> > On 28 Feb, 10:44, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
> > wrote:
> >> Sheldon wrote:
> >>> 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

>
> > 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- Dölj citerad text -
>
> - Visa citerad text -


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

Thanks!

/S

Martin Ambuhl 02-28-2008 03:52 PM

Re: function prototype declaration
 
Sheldon wrote:
> 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.

Keith Thompson 02-28-2008 05:06 PM

Re: function prototype declaration
 
Sheldon <shejo284@gmail.com> writes:
> On 28 Feb, 10:44, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
> wrote:
>> Sheldon wrote:
>> > 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.

[...]
>
> 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.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


All times are GMT. The time now is 12:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.