Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a question about macro

Reply
Thread Tools

a question about macro

 
 
ethan
Guest
Posts: n/a
 
      09-22-2005
Hi All,

I'd like ask some question about macro.
If I define a variable set, such as {5, 2, 10} or {1,3}.

#define X {5,2,10}

1) Is it possible to write a macro to get the number of items within X?

2) Is it possible to get the nth item within X?

e.g.

#define GET_N_ITEM(x, y) ....

GET_N_ITEM({3,2}, 2) is equal to 2.

Thank you very much.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      09-22-2005
In article < .com>,
ethan <> wrote:
>I'd like ask some question about macro.
>If I define a variable set, such as {5, 2, 10} or {1,3}.
>#define X {5,2,10}


There isn't any such thing as a "variable set" in C.
The closest C comes to that is "initializers".

>1) Is it possible to write a macro to get the number of items within X?


Not without dropping in some executable code, such as a temporary
assignment to a variable.

(sizeof (int _list_copy[] = X) / sizeof(int))

>2) Is it possible to get the nth item within X?
>e.g.


>#define GET_N_ITEM(x, y) ....


>GET_N_ITEM({3,2}, 2) is equal to 2.


Not without dropping in some executable code.


There are no list constructs that have independant existance at the
preprocessor level. In C89 at least, you cannot even ask how many
parameters were passed to a macro [but then, C89 doesn't allow any
variation in the number of parameters passed.]
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
 
Reply With Quote
 
 
 
 
ethan
Guest
Posts: n/a
 
      09-23-2005
If I can't write a macro such that get specified item within a define
{x, y, ..., z}, is any other approach can meet such requirement as
follow?

1) I wish there's a some kind of variable set including a set of
positive integer, and it would vary by product.

2) I wish to get some information, such as number of items or value of
the nth item, at the preprocessor level.

How should I do? Thank you very much.

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      09-23-2005
In article < om>,
ethan <> wrote:
>If I can't write a macro such that get specified item within a define
>{x, y, ..., z}, is any other approach can meet such requirement as
>follow?


>1) I wish there's a some kind of variable set including a set of
>positive integer, and it would vary by product.


I don't think I understand what you are asking there.


>2) I wish to get some information, such as number of items or value of
>the nth item, at the preprocessor level.


>How should I do?


I am certain that you cannot do either of those in C89, and
I think it very unlikely you could do it with C99 (but I don't
know C99 as well.)

The preprocessor has no notion at all of "set" or "list" or "array" or
anything even remotely similar. ALL that the preprocessor knows about
is "identifiers" and constants and operators applied to those.

The rules of evaluation of expressions in the preprocessor are pretty
simple: keep replacing macros until you get to a constant or to an
operator or to an identifier that cannot be further macro expanded;
replace defined(name) with a 0 if the name is not defined and a 1 if
the name is defined. If you are within a #if then replace each
identifier (or keyword!) that is left with 0 and evaluate the constant
expression; if you aren't within a #if then whatever was left is
code to be placed at that point.

There is no possibility at all in this evaluation sequence for
indexing or counting.


What you *can* do is something like

#define _l1_1 x
#define _l1_2 y
#define _l1_3 z
#define _l1_4 a
....
#define _l1_26 w

#define _J4(a,b,c,d) a ## b ## c ## d
#define get_nth(var,N) _J4(_,var,_,N)

After that, get_nth(l1,3) would be _l1_3 which would be z .

This isn't array indexing: this is just taking advantage of the
manner in which the preprocessor builds tokens and substitutes
them. It's purely a text-processing trick.


Depending on exactly what you are trying to do, here's a
different approach:

#include <stdio.h>
#define a1(a,b,c) a
#define a2(a,b,c) b
#define a3(a,b,c) c
#define sel3(L, N) (N == 1) ? a1(L) : (N == 2) ? a2(L): a3(L)
#define R 4,1,5
#define T 2,4,6
int main(void) {
printf( "%d\n", sel3(R,1) );
printf( "%d\n", sel3(R,2) );
printf( "%d\n", sel3(R,3) );
printf( "%d\n", sel3(T,1) );
printf( "%d\n", sel3(T,2) );
printf( "%d\n", sel3(T,3) );
return 0;
}


This is completely different from the get_nth approach: sel3(R,1) will
expand *in the code* to (1 == 1) ? 4 : (1 == 2) ? 1: 5
This expression will not be evaluated at the preprocessor level --
not unless you are within a #if .
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
 
Reply With Quote
 
Dave Thompson
Guest
Posts: n/a
 
      10-03-2005
On Thu, 22 Sep 2005 17:57:14 +0000 (UTC),
(Walter Roberson) wrote:

> In article < .com>,
> ethan <> wrote:
> >I'd like ask some question about macro.
> >If I define a variable set, such as {5, 2, 10} or {1,3}.
> >#define X {5,2,10}

>
> There isn't any such thing as a "variable set" in C.
> The closest C comes to that is "initializers".
>

Or in C99 compound literals, or already in GNU C that the equivalent.

> >1) Is it possible to write a macro to get the number of items within X?

>
> Not without dropping in some executable code, such as a temporary
> assignment to a variable.
>
> (sizeof (int _list_copy[] = X) / sizeof(int))
>

That doesn't work, you can't assign an array at all, and sizeof can't
take a declaration, only an expression or typename. You can initialize
an array in a declaration, which at least for auto is executable.

You can just define a variable which you don't use, which doesn't need
to be and shouldn't be auto, and count it:
{ /* in whatever scope handy, 'global'=file if you like */
static /* probably */ int how_many [] = X;
count = sizeof how_many / sizeof (int);
/* since this evaluates at compile time, if there is no other use
of how_many a decent compiler will optimize it away */
}

In C99 you can also do a compound literal, which you similarly don't
(can't) use otherwise:
count = sizeof (int []) X /* is { list } */ / sizeof (int);

> >2) Is it possible to get the nth item within X?
> >e.g.

>
> >#define GET_N_ITEM(x, y) ....

>
> >GET_N_ITEM({3,2}, 2) is equal to 2.

>
> Not without dropping in some executable code.
>
>
> There are no list constructs that have independant existance at the
> preprocessor level. In C89 at least, you cannot even ask how many
> parameters were passed to a macro [but then, C89 doesn't allow any
> variation in the number of parameters passed.]


Even in C99 you can't _ask_, you can only (try to) take all the rest.

- David.Thompson1 at worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dedicated Macro or Normal Macro? John Ortt Digital Photography 5 11-22-2005 12:43 PM
Macro lens on a camera with a macro setting??? mitchell.chris@gmail.com Digital Photography 2 09-28-2005 07:55 AM
in S.E. Asia : Canon EOS 300d with 100 macro ED vs. Nikon D70 with Nikon 105 macro ? J. Cod Digital Photography 0 09-29-2004 05:46 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
macro name from macro? D Senthil Kumar C Programming 1 09-21-2003 07:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57