Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C preprocessor directive problem

  1. Mar 6, 2008 #1
    In the main body of my code i have something like this:

    Code (Text):

    Cmin_vec[1][1][1] = rptr->cmin[3];
     
    I want to replace this with a macro

    Code (Text):

    #define COPY_CMIN( cmin, rptr)  (cmin)[1][1][1] = (rptr)->cmin[3]
     
    and call it in from my code

    Code (Text):

    COPY_CMIN( Cmin_vec, rptr)
     
    But i get the error message saying "structure has no member named `Cmin_vec'.

    how should I go about doing it?
     
  2. jcsd
  3. Mar 6, 2008 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    This expands to
    Code (Text):

    (Cmin_vec)[1][1][1] = (rptr)->Cmin_vec[3]
     
    If you wan't to preserve that cmin[3] on the right-hand side then you must not use cmin as one of the macro argument names. The macro processor replaces all occurrences of a macro parameter in the macro definition text with the corresponding argument value. The expansion is not limited to parameters in parentheses. The preprocessor just sees those parentheses as part of a string.
     
  4. Mar 6, 2008 #3

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    If you want to see macro expansions, the compiler often has an option to let you see input from the "cpp" phase of pre-compilation.
    Code (Text):

    [g]cc -E myfile.c
    will do that. Check your compiler docset or a man page.
    You can also invoke cpp on some UNIX systems directly.
    Make sure the command "which cpp" is able to find the executable image, then try:
    Code (Text):
    cpp myfile.c
    Note that you cannot debug code in a debugger when the code is inside a macro.
     
  5. Mar 6, 2008 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Don't use a macro in the first place. They're evil and generally should never be used.

    - Warren
     
  6. Mar 6, 2008 #5

    CRGreathouse

    User Avatar
    Science Advisor
    Homework Helper

  7. Mar 7, 2008 #6

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    Warren -

    Macros are evil? Is this like void main()? I don't think so. Try reading the the Linux kernel source or the macros that the C standard defines. Macros cause problems when used in a problematic way; the OP's example falls into that category.
     
  8. Mar 7, 2008 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The vast majority of programmers are not skilled enough to properly use macros, or to easily solve the often mystifying bugs they cause. They're just not a good idea. There's almost always a better way.

    - Warren
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C preprocessor directive problem
  1. A C problem (Replies: 3)

  2. C++ problem (Replies: 11)

  3. Problems in C++ (Replies: 5)

  4. A problem with C (Replies: 4)

Loading...