473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Macros and Variables

Hi.. I'm trying to create a macro that accepts 3 variables, one of
which changes when I call it. The problem I'm running in to is that the
third argument is sent as a variable, so it receives it as a letter and
not the corresponding value. here's some code to clarify:

#define some_macro(first, second) dothis(first, something##second)
....

and in my main function I have:

int x = 100;

for(i =0; i < 5; i++)
arr[i] = some_macro(x, i);

Now, the problem is that when the macro resolves, it plugs in the
letter i instead of 0,1,2,3.. etc.

Anyone know a way around this?

Jun 20 '06 #1
4 2227
Cr*****************@gmail.com opined:
Hi.. I'm trying to create a macro that accepts 3 variables, one of
which changes when I call it. The problem I'm running in to is that
the third argument is sent as a variable, so it receives it as a
letter and not the corresponding value. here's some code to clarify:

#define some_macro(first, second) dothis(first, something##second)
...

and in my main function I have:

int x = 100;

for(i =0; i < 5; i++)
arr[i] = some_macro(x, i);

Now, the problem is that when the macro resolves, it plugs in the
letter i instead of 0,1,2,3.. etc.

Anyone know a way around this?


I think I do. Why not use:

dothis(first, something[second]);

instead? If `something[]`s are a mixed bunch, use pointers (e.g. `void
*` which you then cast as needed). Passing `second` into `dothis()`
may help as well.

--
A free society is one where it is safe to be unpopular.
-- Adlai Stevenson

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Jun 20 '06 #2
Well, the thing is that I can't change dothis(first, something##second)
because of the nature of the program. Also, it's actually
something##second##morestuff as it is creating the name of variables.

Any ideas? I hope i'm not too confusing.
Vladimir Oka wrote:
Cr*****************@gmail.com opined:
Hi.. I'm trying to create a macro that accepts 3 variables, one of
which changes when I call it. The problem I'm running in to is that
the third argument is sent as a variable, so it receives it as a
letter and not the corresponding value. here's some code to clarify:

#define some_macro(first, second) dothis(first, something##second)
...

and in my main function I have:

int x = 100;

for(i =0; i < 5; i++)
arr[i] = some_macro(x, i);

Now, the problem is that when the macro resolves, it plugs in the
letter i instead of 0,1,2,3.. etc.

Anyone know a way around this?


I think I do. Why not use:

dothis(first, something[second]);

instead? If `something[]`s are a mixed bunch, use pointers (e.g. `void
*` which you then cast as needed). Passing `second` into `dothis()`
may help as well.

--
A free society is one where it is safe to be unpopular.
-- Adlai Stevenson

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>


Jun 20 '06 #3


Cr*****************@gmail.com wrote On 06/20/06 12:47,:
Hi.. I'm trying to create a macro that accepts 3 variables, one of
which changes when I call it. The problem I'm running in to is that the
third argument is sent as a variable, so it receives it as a letter and
not the corresponding value. here's some code to clarify:

#define some_macro(first, second) dothis(first, something##second)
...

and in my main function I have:

int x = 100;

for(i =0; i < 5; i++)
arr[i] = some_macro(x, i);

Now, the problem is that when the macro resolves, it plugs in the
letter i instead of 0,1,2,3.. etc.

Anyone know a way around this?


Macro processing occurs at compilation time, and the
expanded macro becomes part of your program source. The
values that arise when you run the program are not yet
available when the macro is expanded.

The macro expansion can, of course, produce the names
of variables that will have values at run time, of functions
that will operate on those values at run time, and so on.
But the macro expansion itself cannot involve the value(s)
a variable might acquire someday when the program runs.

--
Er*********@sun.com

Jun 20 '06 #4
Ah, that makes sense. Thanks.

Eric Sosman wrote:
Cr*****************@gmail.com wrote On 06/20/06 12:47,:
Hi.. I'm trying to create a macro that accepts 3 variables, one of
which changes when I call it. The problem I'm running in to is that the
third argument is sent as a variable, so it receives it as a letter and
not the corresponding value. here's some code to clarify:

#define some_macro(first, second) dothis(first, something##second)
...

and in my main function I have:

int x = 100;

for(i =0; i < 5; i++)
arr[i] = some_macro(x, i);

Now, the problem is that when the macro resolves, it plugs in the
letter i instead of 0,1,2,3.. etc.

Anyone know a way around this?


Macro processing occurs at compilation time, and the
expanded macro becomes part of your program source. The
values that arise when you run the program are not yet
available when the macro is expanded.

The macro expansion can, of course, produce the names
of variables that will have values at run time, of functions
that will operate on those values at run time, and so on.
But the macro expansion itself cannot involve the value(s)
a variable might acquire someday when the program runs.

--
Er*********@sun.com


Jun 20 '06 #5

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

Similar topics

21
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...
8
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to...
5
by: davej | last post by:
Hi, I've been working on an opensource project for most of this year. The group has adopted the use of macros in place of function calls when wanting to simplify a function. #define...
5
by: vineoff | last post by:
How can macro take a number in this way (or can it) : #define MYMACRO(x) (x) int val = 5; MYMACRO(val); seems it produces val.
47
by: Emil | last post by:
Is there any hope that new versions of PHP will support macros similar to C or C++? I've searched manual and didn't find anything except define directive, but it can be used to define constant...
17
by: Alex Buell | last post by:
A previous thread about macros just reminded me to ask this. I have avoided using macros and defined some constants i.e.: const int BYTES_PER_TRACK = 10 * 256; const int TRACK0_SS_OFFSET = 0;...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
4
by: barcaroller | last post by:
I was thinking of buying Scott Meyer's second book (More Effective C++) and noticed that it has not been updated since 1995 (unlike his other two famous books). Does anyone know (rumour or...
1
by: lovecreatesbeauty | last post by:
Are these macros defined in Makefile or by some environment which can detect the platform types? /* common code here */ #ifdef LINUX /* other os spefic code */ #elif defined VXWORKS
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.