Macros and Variables

  • Thread starter Cristian.D.Guajardo
  • Start date
C

Cristian.D.Guajardo

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 = 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?
 
V

Vladimir Oka

(e-mail address removed) 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 = 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.
 
C

Cristian.D.Guajardo

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 said:
(e-mail address removed) 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 = 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>
 
E

Eric Sosman

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 = 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.
 
C

Cristian.D.Guajardo

Ah, that makes sense. Thanks.

Eric said:
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 = 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top