Accessing Array Elements

Chapter 14 - Advanced Programming Topics

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Preprocessor Operators
There are three operators that are only available to preprocessor directives. These are the stringize, #, concatenation, ##, and charizing, #@, operators.
# Stringize Operator
Placing a single # in front of a macro parameter causes the compiler to insert the name of the argument instead of its value. This has the overall effect of converting the argument name into a string. The operator is necessary because parameters are not replaced if they occur inside string literals that are explicitly coded in a macro. The following example demonstrates the syntax for the stringize operator:
#define STRINGIZE(ivalue) printf(#ivalue “ is: %d”,ivalue)
    .
    .
    .
int ivalue = 2;
 STRINGIZE(ivalue);
The output from the macro will appear as:
ivalue is: 2
## Concatenation Operator
The concatenation operator is useful when building variable and macro names dynamically. The operator concatenates the items, removing any white space on either side, forming a new token. When ## is used in a macro, it is processed after the macro parameters are substituted and before the macro is examined for any additional macro processing. For example, the following code shows how to create preprocessed variable names:
#define IVALUE_NAMES(icurrent_number) ivalue ## icurrent_number;
    .     
    .
    .
int IVALUE_NAMES(1);
The compiler sees the previous listing as the following declaration:
int ivalue1;
Notice that the preprocessor removed the blanks so that the compiler didn’t see ivalue1 as ivalue 1. The operator can be combined with other preprocessor directives to form complex definitions. The following example uses the concatenation operator to generate a macro name, which causes the preprocessor to invoke the appropriate macro:
#define MACRO1 printf(“MACRO1 invoked.”)
#define MACRO2 printf(“MACRO2 invoked.”)

#define MAKE_MACRO(n) MACRO ## n
    .
    .
    .
MAKE_MACRO(1);
The output from the example will appear as:
MACRO1 invoked.
#@ Charizing Operator
The charizing preprocessor precedes formal parameters in a macro definition. This causes the actual argument to be treated as a single character with single quotation marks around it. For example:
#define CHARIZEIT(cvalue) #@cvalue
    .
    .
    .
cletter = CHARIZEIT(z);
The compiler sees the previous code as:
cletter = ‘z’;

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net