Structure usage from Dennis Ritchie's manual
Below is a copy pasted code from Dennis Ritchie's manual and is regarding the definition of the printf statement. The part what I havent understood throughout this program is usage of 'charpp' and 'doublep' elements of nameless structures in conjunction with an integer pointer 'ap'. Is that even valid?
Code:
printf ( fmt, args )
char fmt [ ] ;
{
char *s;
struct { char **charpp ; };
struct { double *doublep ; };
int *ap, x, c ;
ap = &args ; /* argument pointer */
for ( ; ; ) {
while ( ( c = *fmt++ ) != ´%´ ) {
if(c == ´\0´ )
return ;
putchar(c);
}
switch ( c = *fmt++) {
/* decimal */
case ´d ´:
x = *ap++ ;
if(x < 0) {
x = -x;
if ( x<0 ) { /* is - infinity
printf ( "-32768" ) ;
continue ;
}
putchar ( ´-´);
}
printd(x);
continue ;
/* octal */
case ´o´:
printo ( *ap++);
continue ;
/* float, double */
case ´f ´:
/* let ftoa do the real work */
ftoa ( *ap.doublep++);
continue ;
/* character */
case ´c´:
putchar ( *ap++);
continue ;
/* string */
case ´s´:
s = *ap.charpp++ ;
while ( c = *s++ )
putchar(c);
continue ;
}
putchar(c);
}
}
/**
Print n in decimal ; n must be nonnegative
*/
printd(n)
{
int a ;
if ( a=n/10 )
printd(a);
putchar ( n%10 + ´0´ ) ;
}
/**
Print n in octal, with exactly 1 leading 0
*/
printo(n)
{
if (n)
printo ( ( n>>3 ) &017777 ) ;
putchar ( ( n&07 ) +´0´ ) ;
}
Thanks
Pallavi