C preprocessor directive problem

  • Thread starter Thread starter handsomecat
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the use of C preprocessor macros, specifically addressing an issue encountered when trying to define and use a macro for copying values in a multi-dimensional array. Participants explore the implications of macro expansion and the potential pitfalls associated with using macros in C programming.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an attempt to use a macro to simplify code for copying values from a structure, but encounters an error related to member access.
  • Another participant explains that the macro's parameter name conflicts with the structure member name, leading to unintended expansions during preprocessing.
  • A suggestion is made to use compiler options to view macro expansions, which could help in debugging the issue.
  • One participant expresses a strong opinion against using macros, labeling them as "evil" and suggesting they should generally be avoided.
  • Another participant counters that while macros can be problematic, they can also be useful in certain contexts, indicating a nuanced view on their usage.
  • A further comment emphasizes that the misuse of macros leads to issues, but suggests that skilled programmers can use them effectively.

Areas of Agreement / Disagreement

There is no consensus on the use of macros; while some participants argue against their use, others acknowledge their utility in specific situations. The discussion reflects a mix of technical concerns and philosophical viewpoints regarding programming practices.

Contextual Notes

Participants highlight the importance of understanding macro expansion and the potential for errors when parameter names conflict with member names. The discussion does not resolve the broader debate on the appropriateness of macros in programming.

handsomecat
Messages
68
Reaction score
0
In the main body of my code i have something like this:

Code:
Cmin_vec[1][1][1] = rptr->cmin[3];

I want to replace this with a macro

Code:
#define COPY_CMIN( cmin, rptr)  (cmin)[1][1][1] = (rptr)->cmin[3]

and call it in from my code

Code:
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?
 
Technology news on Phys.org
handsomecat said:
I want to replace this with a macro
Code:
#define COPY_CMIN( cmin, rptr)  (cmin)[1][1][1] = (rptr)->cmin[3]
and call it in from my code
Code:
COPY_CMIN( Cmin_vec, rptr)
This expands to
Code:
(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.
 
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:
[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:
cpp myfile.c

Note that you cannot debug code in a debugger when the code is inside a macro.
 
Don't use a macro in the first place. They're evil and generally should never be used.

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

Similar threads

  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 8 ·
Replies
8
Views
5K
Replies
14
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
12
Views
2K