473,791 Members | 3,179 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 1737
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
2992
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
1914
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
2857
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
3047
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
4261
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
2081
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
1547
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
3399
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
2273
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
9512
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
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10147
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
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9023
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
7531
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
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
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

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.