By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Macros and Variables

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.