Thread: why this warning's? but compiles

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    why this warning's? but compiles

    warning:

    format %s expects type char * , but argument 2 has type char **

    from this piece of code:
    Code:
    #include <stdio.h>
    main()
    {
    	char *colors[3][10] = {'\0'};
    	printf("\nEnter 3 colors seperated by spaces: ");
    	scanf("%s %s %s", colors[0], colors[1], colors[2]);
    	printf("\nYour entered: ");
    	printf("%s %s %s\n", colors[0], colors[1], colors[2]);
    }
    AND

    warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[3]'

    from this piece of code:
    Code:
    #include <stdio.h>
    
    int main( void ) {
    	char tab[2][3] = {'1', '2', '\0', '3', '4', '\0'};
    	printf("%s\n", tab);
    	
    	return 0;
    }
    Thanks!
    Last edited by Salem; 03-14-2010 at 03:40 PM. Reason: Added [code][/code] tags - learn to use them yourself!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, it is expected that you will provide a pointer to char to match with a %s format specifier. However, tab is an array of 2 arrays of 3 char. When you pass it as an argument, it is converted to a pointer to an array of 3 char. Clearly, a pointer an array of 3 char is not a pointer to a char.

    The first one has a similiar problem. You probably want colors to be an array of 3 arrays of 10 chars, i.e.,
    Code:
    char colors[3][10] = {""};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %s expects you to pass a null-terminated string to it (i.e., a pointer to the start of memory where characters are stored). Neither colors[0], colors[1], colors[2], nor tab, is such a thing. colors[0] through [2] are pointers to strings, not strings themselves. tab is a pointer to the beginning of the table, not a pointer to the beginning of a line (which would work -- tab[0] or tab[1] would work for %s).

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    #include <stdio.h>
    main()
    {
    char *colors[3][10] = {'\0'};
    printf("\nEnter 3 colors seperated by spaces: ");
    scanf("%s %s %s", colors[0], colors[1], colors[2]);
    printf("\nYour entered: ");
    printf("%s %s %s\n", colors[0], colors[1], colors[2]);
    }
    Dont you mean char colors[3][10]?
    Code:
    char *colors[3][10] = {'\0'};
    You declared an array of arrays of pointers to char. Doubt you want that. Because you cant store anything in a pointer unless you have mallocated space for him.
    char a[25] = "limited to 25 chars"; //a is alocated
    char *ptr; //space not allocated

    This is what you declared:
    [ptr] [ptr] ... [ptr] //10 of these
    [ptr] [ptr] ... [ptr]
    [ptr] [ptr] ... [ptr]

    Meaning: you didnt allocate space for the the color names. Yet somehow it compiles.

    Also, '\0' is a constant.
    "this is a string" is a string that has \0 (NULL) after 'g' letter.
    a pointer: char *p; can be assigned to point to a string like the string above.
    a pointer to char cannot point to a constant but it can have its value for example.

    because constant itself is a value, not an adress. pointer's point to an adress, not value.
    Last edited by Tool; 03-14-2010 at 02:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Warnings when using vector of vector
    By Boksha in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2008, 01:54 PM
  2. Compilers and warnings
    By rogster001 in forum C Programming
    Replies: 6
    Last Post: 03-26-2008, 05:16 AM
  3. Replies: 9
    Last Post: 03-14-2008, 09:55 AM
  4. Warnings from String manipulation functions.
    By Arker in forum C Programming
    Replies: 4
    Last Post: 10-14-2002, 11:59 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM