function prototype declaration

Discussion in 'C Programming' started by Sheldon, Feb 28, 2008.

  1. Sheldon

    Sheldon Guest

    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
     
    Sheldon, Feb 28, 2008
    #1
    1. Advertising

  2. 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, Feb 28, 2008
    #2
    1. Advertising

  3. 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
     
    Joachim Schmitz, Feb 28, 2008
    #3
  4. Sheldon

    Chris Dollin Guest

    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
     
    Chris Dollin, Feb 28, 2008
    #4
  5. Sheldon

    Sheldon Guest

    On 28 Feb, 10:44, "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;

    >
    > > 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
     
    Sheldon, Feb 28, 2008
    #5
  6. Sheldon wrote:
    > On 28 Feb, 10:44, "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;

    >>
    >>> 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
     
    Joachim Schmitz, Feb 28, 2008
    #6
  7. Sheldon

    Sheldon Guest

    On 28 Feb, 11:08, "Joachim Schmitz" <>
    wrote:
    > Sheldon wrote:
    > > On 28 Feb, 10:44, "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;

    >
    > >>> 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
     
    Sheldon, Feb 28, 2008
    #7
  8. 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.
     
    Martin Ambuhl, Feb 28, 2008
    #8
  9. Sheldon <> writes:
    > On 28 Feb, 10:44, "Joachim Schmitz" <>
    > 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) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 28, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Ward
    Replies:
    6
    Views:
    397
    White Wolf
    Sep 11, 2003
  2. Bhushit Joshipura
    Replies:
    16
    Views:
    3,028
    Dave Hansen
    Jan 13, 2004
  3. Ravishankar S

    function prototype vs function declaration

    Ravishankar S, Dec 18, 2007, in forum: C Programming
    Replies:
    44
    Views:
    1,553
    Flash Gordon
    Dec 23, 2007
  4. June Lee
    Replies:
    2
    Views:
    809
    Jim Cobban
    Apr 13, 2008
  5. Hill Pang
    Replies:
    8
    Views:
    1,126
    Phil Carmody
    Sep 5, 2012
Loading...

Share This Page