473,473 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

a question about macro

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.

Nov 15 '05 #1
4 1449
In article <11**********************@g43g2000cwa.googlegroups .com>,
ethan <op*****@gmail.com> 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
Nov 15 '05 #2
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.

Nov 15 '05 #3
In article <11********************@z14g2000cwz.googlegroups.c om>,
ethan <op*****@gmail.com> 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
Nov 15 '05 #4
On Thu, 22 Sep 2005 17:57:14 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
In article <11**********************@g43g2000cwa.googlegroups .com>,
ethan <op*****@gmail.com> 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
Nov 15 '05 #5

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
2
by: Rex_chaos | last post by:
I have define a MACRO varible as a common variable that be refered by several classes. #ifndef COMMON_VAR #define COMMON_VAR 1 .... #endif
36
by: Peng Jian | last post by:
#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) I'm a beginner learning C. I can't see what *(a) == *(b) is for. If either a or b is a null pointer, this may cause crash. And if...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
23
by: Steven T. Hatton | last post by:
This is one of the first obstacles I encountered when getting started with C++. I found that everybody had their own idea of what a string is. There was std::string, QString, xercesc::XMLString,...
11
by: Remo | last post by:
Hi All I Have Quation About Macro's plz help why do we have # symbol before any macro line #define STOP 0 ... why con't we use the any other symbol like $, @,, plz answer if any one knows...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
6
by: Michael B Allen | last post by:
Hi, I have a macro that looks like the following: #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0 which if used in some code like: MMSG("foo=%s", foo);
3
by: prix prad | last post by:
Hi All, I encountered the above error when I tried to expand a macro as follows: #define EXPAND(array) int M_ ## array The problem occurs when we have 'array' itself as a macro: say...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
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...
0
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.