473,725 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling macros from outside the definition

Ben
This is a follow-on from the "Help with array/pointer segmentation fault needed" thread..

I have not been able to find much information on the rules for macros. I want to be able to call a macro from outside it's
definition which I am fairly sure I have seen done before. This code works:

#define TEST(where,dime nsion,ptr) \
printf ("\tAll %s in %s values: ",where,dimensi on); \
pointer = mArray[bb][cc][rr][vv]; \
while (pointer!=NULL) { \
printf ("%c",pointe r->symbol); \
pointer = pointer->ptr; \
} \
printf("\n");
TEST("prev","bl ock ",prev_in_block );
TEST("prev","co lumn",prev_in_c olumn);
TEST("prev","ro w ",prev_in_r ow);
#undef TEST
But this does not:

#define TEST(where,dime nsion,ptr) \
printf ("\tAll %s in %s values: ",where,dimensi on); \
pointer = mArray[bb][cc][rr][vv]; \
while (pointer!=NULL) { \
printf ("%c",pointe r->symbol); \
pointer = pointer->ptr; \
} \
printf("\n");

#undef TEST
TEST("prev","bl ock ",prev_in_block );
TEST("prev","co lumn",prev_in_c olumn);
TEST("prev","ro w ",prev_in_r ow);

with errors:

[Warning] implicit declaration of function `TEST'
`prev_in_block' undeclared (first use in this function)
`prev_in_column ' undeclared (first use in this function)
`prev_in_row' undeclared (first use in this function)
[Warning] unused variable `pointer'

The reason for this is I have several blocks of code where only the inner loop needs to be macroed, the preceeding lines of each
block are not suitable for macroing.

Can anyone point me to a good reference or just explain what I need to do call a macro from outside it's definition (if it's
possible)?
cheers,

Ben
Jun 16 '06 #1
5 1731
On Fri, 16 Jun 2006 13:00:04 +1000, Ben <be*********@sp am.me> wrote:
This is a follow-on from the "Help with array/pointer segmentation fault needed" thread..

I have not been able to find much information on the rules for macros. I want to be able to call a macro from outside it's
definition which I am fairly sure I have seen done before. This code works:

#define TEST(where,dime nsion,ptr) \
printf ("\tAll %s in %s values: ",where,dimensi on); \
pointer = mArray[bb][cc][rr][vv]; \
while (pointer!=NULL) { \
printf ("%c",pointe r->symbol); \
pointer = pointer->ptr; \
} \
printf("\n");
TEST("prev","bl ock ",prev_in_block );
TEST("prev","co lumn",prev_in_c olumn);
TEST("prev","ro w ",prev_in_r ow);
#undef TEST
But this does not:

#define TEST(where,dime nsion,ptr) \
printf ("\tAll %s in %s values: ",where,dimensi on); \
pointer = mArray[bb][cc][rr][vv]; \
while (pointer!=NULL) { \
printf ("%c",pointe r->symbol); \
pointer = pointer->ptr; \
} \
printf("\n");

#undef TEST
TEST("prev","bl ock ",prev_in_block );
How could it possibly work? The #undef removes the macro. At this
point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and trying to
reference it outside the block. It no longer exists once you leave
the block. TEST("prev","co lumn",prev_in_c olumn);
TEST("prev","ro w ",prev_in_r ow);

with errors:

[Warning] implicit declaration of function `TEST'
`prev_in_block' undeclared (first use in this function)
`prev_in_column ' undeclared (first use in this function)
`prev_in_row' undeclared (first use in this function)
[Warning] unused variable `pointer'

The reason for this is I have several blocks of code where only the inner loop needs to be macroed, the preceeding lines of each
block are not suitable for macroing.

Can anyone point me to a good reference or just explain what I need to do call a macro from outside it's definition (if it's
possible)?
cheers,

Ben

Remove del for email
Jun 16 '06 #2
Ben
Barry Schwarz wrote:

How could it possibly work? The #undef removes the macro. At this
point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and trying to
reference it outside the block. It no longer exists once you leave
the block.

Ok so it blanket can't be done and I need to write a separate function (I get this is exactly what functions are for).

Funny though...I have a computer science textbook that repeatedly refers to a macro definition for creating structures, where
the labels of members are given to the macro. Now since they are only doing this once per 'program', what's the point of having
a macro? It's just a regular definition of a structure. I guess they were just trying to save space in the textbook, but I took
it to mean they were calling it from outside the definition.

thanks
Jun 16 '06 #3

Ben wrote:
Barry Schwarz wrote:

How could it possibly work? The #undef removes the macro. At this
point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and trying to
reference it outside the block. It no longer exists once you leave
the block.

Ok so it blanket can't be done and I need to write a separate function (I get this is exactly what functions are for).

Funny though...I have a computer science textbook that repeatedly refers to a macro definition for creating structures, where
the labels of members are given to the macro. Now since they are only doing this once per 'program', what's the point of having
a macro? It's just a regular definition of a structure. I guess they were just trying to save space in the textbook, but I took
it to mean they were calling it from outside the definition.

thanks


just remove the #undef for the macro TEST

Jun 16 '06 #4
Ben wrote:
Barry Schwarz wrote:
How could it possibly work? The #undef removes the macro. At
this point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and
trying to reference it outside the block. It no longer exists
once you leave the block.


Ok so it blanket can't be done and I need to write a separate
function (I get this is exactly what functions are for).


I think you are confused about what terminates the macro
definition. That is done by the end of the line doing the
definition, which is why you have the line continuation
operations. #undef discards the whole definition - gone, killed,
never to chirp again.

--
"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jun 16 '06 #5
Ben
CBFalconer wrote:
Ben wrote:
Barry Schwarz wrote:
How could it possibly work? The #undef removes the macro. At
this point in the code, the macro TEST no longer exists.

This is pretty similar to defining a variable in a block and
trying to reference it outside the block. It no longer exists
once you leave the block.

Ok so it blanket can't be done and I need to write a separate
function (I get this is exactly what functions are for).


I think you are confused about what terminates the macro
definition. That is done by the end of the line doing the
definition, which is why you have the line continuation
operations. #undef discards the whole definition - gone, killed,
never to chirp again.

Not confused, didn't know! If you look at the other thread someone else provided the code for the macro. But thanks for
providing the answer.
cheers,
Ben
Jun 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2989
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can spare the time, I would appreciate any comments (including words like evil and disgusting, if you think they are applicable :-}) on the example here. Kenny -
1
1909
by: Rafal 'Raf256' Maj | last post by:
Hi, is there a commend to undefine all macros #define inside a file? in example: #define FOR(x) for (..............) #define MyMacro1(x,y) ........... // ... code ...
11
2850
by: Ben Hetland | last post by:
....in certain cituations they can be useful if not for anything else, then at least for saving a lot of repetetetetetitititive typing. :-) Beyond the point of "do something better instead", I'm curious about how the following syntactical problem can be solved. It should apply equally to C and C++ as it mainly is a preprocessor-related problem. I tryed to define something similar to the following example: ...
37
3041
by: hasadh | last post by:
Hello, probably this may be a simple qn to u all but I need an answer plz. In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who included this code as a library into his module said that in his code he couldnt use the above words as function names or variable names and got compilation errors. He advised me to use them as enums instead of macros. is he right ??? (in my code the macros are used as return values from fns...
19
4257
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
8
2078
by: David | last post by:
Hi, I am using header file for driver so Can I define macro to call Kernel function ? What are the generatl rules for defining macro. - David
1
1545
by: n4ixt | last post by:
I have two macros I used to use in VS 2003. I recently tried to import them for use in VS 2005, but they don't seem to work. I open the macro explorer, right click and do run, but it's like VS never calls the things. What the heck am I doing wrong? Maybe it's just the late hour but I've been going in circles trying to figure it out. Here's the entire macro module below (be on the look out in case of any word wrap), any help is...
20
3390
by: copx | last post by:
Is it true that C macros are unsafe when combined with side effects even if they are "clean" e.g. could foo_macro(++c); execute "++c" multiple times even if foo_macro is "clean"? I have always thought that "clean" macros (i.e. macros where all parameters are surrounded by parentheses in the definition) do not have this problem... I am wondering because I have just looked up putc() in a reference, and it
5
2271
by: Joakim Hove | last post by:
Hello, I have a function like this: /* It actually does something else .... */ void func (int size, const char ** string_list) { int i; for (i=0; i < size; i++) printf("String number %d: %s \n",i,string_list);
0
8751
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8095
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4781
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3219
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2156
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.