Thread: For loop controller using pointers

  1. #1
    Registered User Pat Wanjau's Avatar
    Join Date
    Jan 2015
    Location
    Nairobi, Kenya
    Posts
    2

    Question For loop controller using pointers

    I'm familiar with the concept of int in the for loop. However, I've seen a case where the condition for the for loop is a pointer. How is the pointer used or interpreted in the for loop condition in C or C++. As an example:

    Code:
    void printer(char *fmt, ...) {
        va_list args;
        char *p, *sval;
        int ival;
        double dval;
    
        va_start(args, fmt);
        for(p = fmt; *p; p++) {
            if(*p != '%') {
                putchar(*p);
                continue;
            }
            switch(*++p) {
                case 'd':
                    ival = va_arg(args, int);
                    printf("%d", ival);
                    break;
                case 'f':
                    dval = va_arg(args, double);
                    printf("%.2f", dval);
                    break;
                case 's':
                    for(sval = va_arg(args, char*); *sval; sval++)
                        putchar(*sval);
                    break;
                default:
                    putchar(*p);
            }
        }
        va_end(args); // cleanup when done
    }
    This example is derived from the book:
    The C programming Language
    By Brian W. Kernighan and Dennis M. Ritchie.
    Published by Prentice-Hall in 1988
    ISBN 0-13-110362-8 (paperback)
    ISBN 0-13-110370-9

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Woah, Database Error is becoming some irritating thing.

    Anyways, as you increment the pointer, it points to a string one character less, and eventually points to nothing hence the condition-expression eventually evaluates to false in this way.

    Same as you could do:

    Code:
    char *string = NULL;
    
    if(string) { //evaluates to false
    }
    
    char *name = "Something";
    
    if(name) { //evaluates to true
    
    }
    
    /* dereferencing a pointer to a string gives you the first character in the string  */
    
    if (*name) { //evaluates to true
     }
    
    if (*string) { //evalutes to false 
    }
    Last edited by Aslaville; 01-04-2015 at 01:10 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(p = fmt; *p; p++)
    Is read as
    for(p = fmt; *p != '\0' ; p++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Pat Wanjau's Avatar
    Join Date
    Jan 2015
    Location
    Nairobi, Kenya
    Posts
    2
    Thank you so much Aslaville &Salem.​ That helped so much, and I now understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Pointers in a loop
    By Or Ellenbogen in forum C Programming
    Replies: 3
    Last Post: 06-28-2013, 06:04 AM
  2. Pointers in a for loop
    By Edelweiss in forum C Programming
    Replies: 6
    Last Post: 09-16-2011, 01:38 AM
  3. running through a loop using pointers
    By boy in forum C Programming
    Replies: 43
    Last Post: 08-19-2011, 04:43 PM
  4. Initialising pointers using a loop.
    By will of fortune in forum C Programming
    Replies: 4
    Last Post: 03-13-2010, 05:17 AM
  5. Loop Help with Pointers.
    By 1BadRbt in forum C Programming
    Replies: 3
    Last Post: 12-20-2006, 01:36 AM

Tags for this Thread