Thread: printf format problem

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    printf format problem

    i was following a tutorial at:Learn C The Hard Way A Clear & Direct Introduction To Modern C Programming

    on exercise 15 i am trying the extra credit for rewriteing the program using functions.

    here is my code:
    Code:
    #include <stdio.h> 
    
    void indexarrayPrint();
    
    
    int pointerPrint1( int *x, char **y);
    
    
    int pointerPrint2( int *x, char **y);
    
    
    int pointerPrint3( int *x, char **y);
    
    
    int pointerPrint4( int *x, char **y);
    
    
    int main(int argc, char *argv) 
    {
        int ages[] = {23, 43, 12, 89, 2};
        char *names[] = {
            "Alan", "Frank",
            "Mary", "John", "Lisa"
        };
    
    
        int *cur_age = ages;
        char **cur_name = names;
    
    
        indexarrayPrint(names, ages);
        pointerPrint1( cur_age, cur_name);
        pointerPrint2( cur_age, cur_name);
        pointerPrint3( cur_age, cur_name);
        pointerPrint4( cur_age, cur_name);    
    
    
        return 0;
    }
    
    
    
    
    void indexarrayPrint()
    {
        int ages[] = {23, 43, 12, 89, 2};
        char *names[] = {
            "Alan", "Frank",
            "Mary", "John", "Lisa"
        };
        
    
    
        printf("indexarrayPrint: \n");    
        int count = sizeof(ages) / sizeof(int);
        int i = 0;
        
        for(i = 0; i < count; i++) {
            printf("%s has %d years alive.\n", names[i], ages[i]);
        }
        printf("---\n");
    
    
    }
    
    
    int pointerPrint1( int *x, char **y)
    {
        printf("pointerPrint1: \n");
        int ages[] = {23, 43, 12, 89, 2};
        
        int count = sizeof(ages) / sizeof(int);
        int i = 0;
        
        for(i = 0; i < count; i++) {
            printf("%s is %d years old.\n", *(y+i), *(x+i));
        }
        
        printf("---\n");
    }
    
    
    int pointerPrint2( int *x, char **y)
    {
        printf("pointerPrint2: \n");
        int ages[] = {23, 43, 12, 89, 2};
        
        int count = sizeof(ages) / sizeof(int);
        int i = 0;
    
    
        for (i = 0; i < count; i++) {
            printf("%s is %d years old again.\n", y[i], x[i]);
        }
        
        printf("---\n");
    }
    
    
    
    
    /* This Prints Garbadge... WIP */
    int pointerPrint3( int *x, char **y)
    {
        printf("pointerPrint3: \n");
        int ages[] = {23, 43, 12, 89, 2};
        char *names[] = {
            "Alan", "Frank",
            "Mary", "John", "Lisa"
        };
        
    
    
        int count = sizeof(ages) / sizeof(int);
        int i = 0;
    
    
        for (y = names, x = ages; (x - ages) < count; x++, y++) {
            printf("%s lived %d years so far.\n", y, x);
        }
        
        printf("---\n");
    }
    
    
    int pointerPrint4( int *x, char **y)
    {
        printf("pointerPrint4: \n");
        int ages[] = {23, 43, 12, 89, 2};
        
        int count = sizeof(ages) / sizeof(int);
        int i = 0;
        
        for (i = 0; i < count; i++) {
            printf("%p lived %p years ago...\n", &y, &x);
        }
        
        printf("---\n");
    }
    it works but i got 2 warnings:
    Code:
    ex15_function.c: In function ‘pointerPrint3’:ex15_function.c:102:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
    ex15_function.c:102:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat]
    the function pointerPrint3 gives garbade output in the %s and %d areas, how do i format this to get rid of the warnings and have it print correctly?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    x and y are pointers to the things you want to give printf, i.e. a pointer to an int and a pointer to a string (pointer to a char pointer). printf just wants a plain int and a plain string (char pointer). You need to dereference them to get the thing they point at. Put a * in front of each of them:
    Code:
    printf("%s lived %d...", *y, *x);

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    Thank You, its all good now, i will keep that in my memory for the future.

    I am glad i asked, i was kinda embarrassed i was getting this kind of error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incorrect format using printf()
    By Char*Pntr in forum C Programming
    Replies: 15
    Last Post: 09-18-2010, 04:44 AM
  2. printf format specifier padding
    By Blasz in forum C Programming
    Replies: 3
    Last Post: 07-08-2010, 11:26 PM
  3. printf trying to format numbers....
    By Dragonslayre in forum C Programming
    Replies: 3
    Last Post: 07-07-2007, 11:55 PM
  4. Why is *format a pointer in printf()?
    By ILFactotum in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2007, 05:44 PM
  5. format printf
    By Shila in forum C Programming
    Replies: 2
    Last Post: 11-23-2002, 03:14 PM

Tags for this Thread