printf
Wade Richards
t-wader at microsoft.UUCP
Tue Oct 24 11:52:13 AEST 1989
In article <543 at uwm.edu> zhao at csd4.csd.uwm.edu (T.C. Zhao) writes:
=}I recently came across a piece of c code:(both a and b are integers)
=}printf("%d"+(a),b);
=}in passes compiler without any problem, what does this code mean ?
=}
=}--
=}----------------------------------------------
=}Internet: zhao at csd4.csd.uwm.edu
=} BITNET: zhao%csd4.csd.uwm.edu at WISCMAC3.BITNET
That code is the same as:
switch( a ) {
case 0: printf( "%d", b ); break;
case 1: printf( "d", b ); break;
case 2: printf( "", b ); break;
default: printf( (char *)random(), b ); break;
}
The expression "%d"+(a) evaluates to a pointer to the string "%d\0" somewhere
in memory, plus the integer a. The sum of a "pointer to type X" and an integer
is another "pointer to type X", pointing to the a-th following object of type
X. This is not at all clear. By way of example, if the pointer pointed to
memory location 1000, and an object of type X took up 5 bytes, then the pointer
plus 2 would be: 1000 + 2*5 = 1010.
Hope this helps.
--- Wade
BYW, the (char *)random() simply means a (lot likely valid) pointer to anywhere.
More information about the Comp.lang.c
mailing list