Structure usage from Dennis Ritchie's manual

This is a discussion on Structure usage from Dennis Ritchie's manual within the C Programming forums, part of the General Programming Boards category; Below is a copy pasted code from Dennis Ritchie's manual and is regarding the definition of the printf statement. The ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    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

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    from your code, i guess you are looking at the Bell Labs online c programming reference manual? if so, you are looking at a description of a really early version of C that allowed all sorts of things that are no longer valid. so the answer is no, that is not valid in ANSI C or even most older C implementations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lexical convention from Dennis Ritchie's manual
    By goodluck in forum C Programming
    Replies: 3
    Last Post: 01-04-2012, 10:46 AM
  2. Dennis M. Ritchie
    By kermit in forum General Discussions
    Replies: 41
    Last Post: 10-17-2011, 04:39 PM
  3. The Father of C has Died Dennis Richie
    By zerokernel in forum C Programming
    Replies: 1
    Last Post: 10-14-2011, 09:15 AM
  4. Kernighan and Ritchie: Gods
    By Vespasian in forum Programming Book and Product Reviews
    Replies: 4
    Last Post: 09-25-2011, 11:41 PM
  5. Kerningham Ritchie - example query
    By aynzoya in forum C Programming
    Replies: 3
    Last Post: 02-20-2009, 12:25 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21