-1

How would I define multiple values of the same type in an array using #define ?

For instance, I would like

#define DIGIT 0x30 | 0x31 | 0x32 | 0x33 | 0x34 | 0x35 | 0x36 | 0x37 | 0x38 | 0x39
#define QUOTE 0x22 | 0x27

ETC...
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Frank
  • 3
  • 1
  • 1
  • 2
  • 6
    Huh? What on earth are you trying to do? – SLaks Jul 04 '11 at 01:57
  • Did you try that? That should work. What's the problem? – Jonathan Wood Jul 04 '11 at 01:57
  • 3
    @Jonathan: Passing the preprocessor without error, and *working as desired* are two different things. – Ben Voigt Jul 04 '11 at 02:00
  • C? C++? What's it to be? – Kerrek SB Jul 04 '11 at 02:01
  • 1
    @Jonathan Uh, what? '|' is OR in C, which surely isn't what the OP wants or is asking for. – Jim Balter Jul 04 '11 at 02:02
  • 2
    @Adrian: Show an example of how you want to use one of these "array macros". – Ben Voigt Jul 04 '11 at 02:06
  • @Jim and @Ben: Thanks for the lessons is simple logic. Is it too much to ask anymore than those asking questions on stackoverflow clarify what they are trying to do or why what they've tried wasn't right? – Jonathan Wood Jul 04 '11 at 05:23
  • @Jonathan P.S. A major reason that people ask unclear questions is that they misunderstand the language or have mistaken assumptions about it and don't realize that their questions are unclear. Also some people asking questions here are barely out of diapers and lack the experience and language skills to ask good questions. So it's too much to *expect* clarity in every case. OTOH, it should not be too much to expect more experienced hands to see beginners' errors and to refrain from claiming that ORing a bunch of character values "should work" for ANYTHING that the OP is trying to do. – Jim Balter Jul 07 '11 at 05:09
  • 1
    @Jonathan Your second rude and offensive comment earns a flag. In addition to that, it employs a textbook ad hominem fallacy. – Jim Balter Jul 07 '11 at 06:18

4 Answers4

11

How would I define multiple values of the same type in an array using #define ? For instance, I would like

#define DIGIT 0x30 | 0x31 | 0x32 | 0x33 | 0x34 | 0x35 | 0x36 | 0x37 | 0x38 | 0x39
#define QUOTE 0x22 | 0x27

Well, the term array in C and C++ refers to a number of variables of the same type, all side by side in memory. If you really wanted an array, then you can use:

static const char digits[] = {
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39
};

Of course, there's nothing to stop you putting part of this in a preprocessor macro, but no obvious point either, and macros are best avoided as conflicting and unintended substitutions aren't always handled well by the compiler:

#define DIGITS 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39
static const char digits[] = { DIGITS };

If your intension it to check whether a particular character is one of the listed characters, then you can do it in many ways:

if (isdigit(c)) ...    // use a library function

static const char digits[] = {
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0
};
if (strchr(digits, c)) ...  // use another library function (probably slower)

static const char digits[] = "0123456789";  // exactly the same as above!
if (strchr(digits, c)) ...

if (c == 0x30 || c == 0x31 || c == 0x32 ...) ...    // painfully verbose!

if (c == '0' || c == '1' || c == '2' ...) ... // verbose but self-documenting

if (c >= '0' && c <= '9')  // as per caf's comment, C requires the
                           // character set to have contiguous numbers
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
4

You don't, you use an enum or an array.

typedef enum
{
    Zero  = 0x30,
    One   = 0x31,
    Two   = 0x32,
    Three = 0x33,
    /* etc. */
} digits;

The #define in your example is simply the result of all those bitwise OR operations.

To solve this using an array (as it would actually work well considering the indices would line up with the digits), well... just declare one! =)

You don't need to (and likely don't want to) use the preprocessor for that. Just declare a static const array:

static const char digits[] = { 0x30, 0x31, 0x32 /* etc */ };

int main(...) {
    char zero = digits[0];
    char one  = digits[1];
    char two  = digits[2];
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
3

You might want to consider using an enum to specify these values, and using well known functions, such as isdigit(int) (ctype.h).

Not sure of a standard C method for quotes (one probably exists though), but ORing those values together is probably not what you want anyway.

pickypg
  • 22,034
  • 5
  • 72
  • 84
  • I was trying to use the preprocessor for defining values such as DIGIT, ALPHA, ALPHANUM, PUNCUATION, ETC... ALSO... isdigit(int) is a preprocessor macro, so if they did that, it shouldn't be to hard to implement this... – Frank Jul 04 '11 at 02:10
  • Something close to ORing could be : #define DIGIT(d) ((d^0x30)<0x10) – BenjaminB Jul 04 '11 at 02:20
  • @Adrian: `isdigit()` is a function. not a macro. The function is declared in ``. – pmg Jul 04 '11 at 10:13
  • @Frank Your reasoning is mistaken. Even if `isdigit` were a preprocessor macro, that fact would not say anything about the ease of implementing your defines. – Jim Balter Jul 07 '11 at 05:25
  • @Frank P.S. "defining values such as" doesn't say enough because you haven't specified the context in which you want to use them. e.g., there is no definition of DIGIT that will make if(c == DIGIT) work, but there is a definition that would make switch(c) { DIGIT: ... } work. Your example using '|' and your comment about isdigit suggests some fundamental misunderstandings about C and programming generally; I suggest getting some in-depth instruction, perhaps by reading some good programming books. – Jim Balter Jul 07 '11 at 05:33
2

You don't. You use static const variables. Sorry to be pendantic, but stay away from the preprocessor.

El Marcel
  • 1,767
  • 1
  • 10
  • 11