Thread: Problem with defining function

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    55

    Problem with defining function

    ok here's what i have done;

    #define a(r) ((4)*(1/r)*(1-(1/r)));

    using it like this:

    tot = tot + a(r);

    But while compiling i get :

    invalid operands to binary +

    Can anyone suggest me what am i doing wrong?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've got an error that doesn't cause an error (here): this line now ends in two semicolons (one from the #define, one from the line itself).

    What is r, then? I hope it's a floating-point thing, otherwise 1/r could get tricky.

    And why not use a function instead of a pseudofunction?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    55
    r is double and i mistype it i don't have ";" in my real code and 4 is 4.0 and 1 is 1.0 is my code. I am just trying to this pseudofunction .

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    You've got an error that doesn't cause an error (here): this line now ends in two semicolons (one from the #define, one from the line itself).
    What tabstop means is don't put a semi-colon after a #define statement.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    55
    r is double and i mistype it i don't have ";" in my real code and 4 is 4.0 and 1 is 1.0 is my code. I am just trying to this pseudofunction .

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by curious View Post
    r is double and i mistype it i don't have ";" in my real code and 4 is 4.0 and 1 is 1.0 is my code. I am just trying to this pseudofunction .
    Cut, paste, and post the appropriate parts of your real code then.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    55
    #define a(r) (-4.0)*(1.0/(r))*((1.0/(r))-1.0)

    And there is subroutine
    which uses this function

    tot = tot + a(r);

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Minimum sample

    Code:
    #define a(r) (-4.0)*(1.0/(r))*((1.0/(r))-1.0)
    
    int main(void)
    {
        double tot = 0;
        double r = 10;
        tot = tot + a(r);
        return 0;
    }
    Compiles fine.

    Are you sure there are not problems on previous lines? Are you sure the compiler error is referring to this line? Are you sure your types are OK, e.g tot is not char* or whatever?
    Last edited by anon; 06-01-2009 at 04:50 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    55
    I have also called the same function in main and it's works fine but when i tired to call it in a subroutine it didn't work at all

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by curious View Post
    I have also called the same function in main and it's works fine but when i tired to call it in a subroutine it didn't work at all
    Cut, paste, and post the subroutine where you call it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And remember, the file is parsed from top to bottom, so attempting to call a macro above where it is defined simply won't work.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    55
    @tabstop: how to do it then?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Define it before you use it. (This seems a bit too obvious, but is correct nevertheless.)

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    55
    ok the basic structure is :
    Code:
    #define ......
    
    int main()
    {
    }
    
    bool func() /**bool is taken care of and works fine**/
    {
    
       defined function is called here as explained above
    }

  15. #15
    Registered User
    Join Date
    May 2008
    Posts
    55
    @tabstop :defined it but it didn't work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  2. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM