Profile

ys2310

Author:ys2310
2008年春にNew York Cityにあるふる〜い大学を卒業。


Categories


new postings


new comments


new trackbacks


monthly archeive


FC2ブログ 転職
DATE: CATEGORY:スポンサー広告
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
| BLOG TOP |
DATE: CATEGORY:C/C++ operator
char*p="Hello";
printf("p[2]\n");
printf(p);
printf(&p[2]);
printf(&2[p]);
printf(p+2);

printf("%d\n",p);
printf("%d\n",&p[2]);
printf("%d\n",&2[p]);
printf("%d\n",p+2);
printf("%d\n",p[2]);
printf("%d\n",&p);
printf("%d\n",&p+2);

以上からわかるように、& 演算子は右辺のlvalue のaddress を返します。

&p[2] はその配列要素のaddress を返し、
&p はpointer p のheap 上のlocal address を教えてくれる。

&(p + 2) はcomile error となります。

もっともな理由は右辺がlvalue じゃないからなのですが、よく考えると
p + 2 はaddress であって &(p + 2)、つまりそのaddress(p + 2)を指している address
を訊ねていてるのです。もちろん、そんなpointer はありませんし、そもそあるも配列の途中要素を指しているpointer なんて聞いたことないし、定義出るわけもない。

| BLOG TOP |
DATE: CATEGORY:C/C++ operator
The number-sign or "stringizing" operator (#) converts macro parameters to string literals without expanding the parameter definition. It is used only with macros that take arguments. If it precedes a formal parameter in the macro definition, the actual argument passed by the macro invocation is enclosed in quotation marks and treated as a string literal. The string literal then replaces each occurrence of a combination of the stringizing operator and formal parameter within the macro definition.
[Note] Note:

The Microsoft C (versions 6.0 and earlier) extension to the ANSI C standard that previously expanded macro formal arguments appearing inside string literals and character constants is no longer supported. Code that relied on this extension should be rewritten using the stringizing (#) operator.

White space preceding the first token of the actual argument and following the last token of the actual argument is ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal. Thus, if a comment occurs between two tokens in the actual argument, it is reduced to a single white space. The resulting string literal is automatically concatenated with any adjacent string literals from which it is separated only by white space.

Further, if a character contained in the argument usually requires an escape sequence when used in a string literal (for example, the quotation mark (") or backslash (\) character), the necessary escape backslash is automatically inserted before the character.

The Visual C++ stringizing operator may not behave as expected in all situations; see 16.3.2 The # Operator for more information.
Example

The following example shows a macro definition that includes the stringizing operator and a main function that invokes the macro:

Such invocations would be expanded during preprocessing, producing the following code:

int main() {
printf_s( "In quotes in the printf function call\n" "\n" );
printf_s( "\"In quotes when printed to the screen\"\n" "\n" );
printf_s( "\"This: \\\" prints an escaped double quote\"" "\n" );
}

// stringizer.cpp
#include
#define stringer( x ) printf_s( #x "\n" )
int main() {
stringer( In quotes in the printf function call );
stringer( "In quotes when printed to the screen" );
stringer( "This: \" prints an escaped double quote" );
}

In quotes in the printf function call
"In quotes when printed to the screen"
"This: \" prints an escaped double quote"

The following sample shows how you can expand a macro parameter:

// stringizer_2.cpp
// compile with: /E
#define F abc
#define B def
#define FB(arg) #arg
#define FB1(arg) FB(arg)
FB(F B)
FB1(F B)

[Quote]:http://msdn2.microsoft.com/en-us/library/7e3a913x.aspx

| BLOG TOP |
DATE: CATEGORY:C/C++ operator
The double-number-sign or "token-pasting" operator (##), which is sometimes called the "merging" operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition.

If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement.

Then, each occurrence of the token-pasting operator in token-string is removed, and the tokens preceding and following it are concatenated. The resulting token must be a valid token. If it is, the token is scanned for possible replacement if it represents a macro name. The identifier represents the name by which the concatenated tokens will be known in the program before replacement. Each token represents a token defined elsewhere, either within the program or on the compiler command line. White space preceding or following the operator is optional.

This example illustrates use of both the stringizing and token-pasting operators in specifying program output:

#define paster( n ) printf_s( "token" #n " = %d", token##n )
int token9 = 9;

If a macro is called with a numeric argument like

paster( 9 );

the macro yields

printf_s( "token" "9" " = %d", token9 );

which becomes

printf_s( "token9 = %d", token9 );

Example

// preprocessor_token_pasting.cpp
#include
#define paster( n ) printf_s( "token" #n " = %d", token##n )
int token9 = 9;

int main()
{
paster(9);
}

token9 = 9

[Quote]:http://msdn2.microsoft.com/en-us/library/09dwwt6y.aspx

| BLOG TOP |
DATE: CATEGORY:C/C++ operator
Microsoft Specific

The charizing operator can be used only with arguments of macros. If #@ precedes a formal parameter in the definition of the macro, the actual argument is enclosed in single quotation marks and treated as a character when the macro is expanded. For example:

#define makechar(x) #@x

causes the statement

a = makechar(b);

to be expanded to

a = 'b';

The single-quotation character cannot be used with the charizing operator.

[Quote]:http://msdn2.microsoft.com/en-us/library/91tt6dfs.aspx

| BLOG TOP |

copyright © Manhattan life all rights reserved.Powered by FC2ブログ