C preprocessor directive problem

  • Thread starter handsomecat
  • Start date
In summary: 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.
  • #1
handsomecat
70
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
  • #2
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.
 
  • #3
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.
 
  • #4
Don't use a macro in the first place. They're evil and generally should never be used.

- Warren
 
  • #6
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.
 
  • #7
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
 

1. What is a C preprocessor directive?

A C preprocessor directive is a command that is used to manipulate code before it is compiled. It is used to perform tasks such as including header files, defining constants, and conditional compilation.

2. What is a common problem encountered with C preprocessor directives?

A common problem encountered with C preprocessor directives is the use of macros. If not used carefully, macros can lead to unexpected behavior and errors in the code.

3. How can I avoid errors caused by preprocessor directives?

To avoid errors caused by preprocessor directives, it is important to use them correctly and carefully. This includes using conditional compilation only when necessary, avoiding redefinition of macros, and using #undef to remove macros when no longer needed.

4. Can preprocessor directives be nested?

Yes, preprocessor directives can be nested. This means that a preprocessor directive can contain another preprocessor directive within it, as long as it is properly terminated.

5. Are there any alternatives to using C preprocessor directives?

Yes, there are alternatives to using C preprocessor directives such as inline functions and templates. These alternatives can help avoid some of the issues associated with preprocessor directives, but each has its own limitations and trade-offs.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
308
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top