printf turns pointer into type int when %s expects type char

This is a discussion on printf turns pointer into type int when %s expects type char within the C Programming forums, part of the General Programming Boards category; However, a similar trick actually is useful, and that's using a string literal as the object for subscripting: hmm... but ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    However, a similar trick actually is useful, and that's using a string literal as the object for subscripting:
    hmm... but that seems to be more like a special case of a[n] than n[a], since a string is an array of chars.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but that seems to be more like a special case of a[n] than n[a]
    I'd say it's more of a case of fully understanding The Rule. It's a similar trick in that it uses subscripting, because I couldn't think of any good reason to use n[a].
    My best code is written with the delete key.

  3. #18
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Nobody's mentioned the potential out-of-bounds indexing:
    Code:
    char *article[ 5 ] = { "the", "a", "one", "some", "any" };
    char *noun[ 5 ] = { "boy", "girl", "dog", "town", "car" };
    char *verb[ 5 ] = { "drove", "jumped", "ran", "walked", "skipped" };
    char *preposition[ 5 ] = { "to", "from", "over", "under", "on" };
    
    ...
    
    printf( "%s %s %s %s %s %s\n", 
            article[ rand() % 5 + 1 ],
            noun[ rand() % 5 + 1 ],
            verb[ rand() % 5 + 1 ],
            preposition[ rand() % 5 + 1 ],
            article[ rand() % 5 + 1 ],
            noun[ rand() % 5 + 1 ] );
    rand() % 5 is a value between 0 and 4. Adding 1 to that yields a range from 1 to 5. The valid array indexes are 0 to 4. And try to avoid magic numbers.

    Try:
    Code:
    char *article[] = { "the", "a", "one", "some", "any" };
    char *noun[] = { "boy", "girl", "dog", "town", "car" };
    char *verb[] = { "drove", "jumped", "ran", "walked", "skipped" };
    char *preposition[] = { "to", "from", "over", "under", "on" };
    
    srand( time( NULL ) );
    
    printf( "%s %s %s %s %s %s\n", 
            article[ rand() % sizeof(article)/sizeof(*article) ],
            noun[ rand() % sizeof(noun)/sizeof(*noun) ],
            verb[ rand() % sizeof(verb)/sizeof(*verb) ],
            preposition[ rand() % sizeof(preposition)/sizeof(*preposition) ],
            article[ rand() % sizeof(article)/sizeof(*article) ],
            noun[ rand() % sizeof(noun)/sizeof(*noun) ] );
    That way, you can change the arrays to have however many string values in them as you want and the compiler will figure everything out regarding sizes automatically without you needing to change any other code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 07:35 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

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