Thread: Incompatible Pointer Type Warning

  1. #1
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59

    Incompatible Pointer Type Warning

    Code:
    StringSort.c:25: warning: passing arg 1 of `printDat' from incompatible pointer type
    for the following code:


    Code:
    #include <stdio.h>
    #include <string.h>
    
    void printDat(char *[]);
    
    int main()
    {
            int i;
    
            char stringPointers[10][50] = {  "Hello!\0",
                                             "Happy Halloween!\0",
                                             "How are you?\0",
                                             "1337\0",
                                             "Coffee is good.\0",
                                             "r00t\0",
                                             "C is efficient.\0",
                                             "Half-Life 2 rocks.\0",
                                             "So does Star Wars.\0",
                                             "programmingSkills == \"$$$\"\0"};
    
            char *namePtr[10];
            for(i = 0; i < 10; i++)
                    namePtr[i] = stringPointers[i];
    
            printDat(stringPointers);
    
            return 0;
    }
    
    void printDat(char *str[])
    {
            printf("\n\nstr = %s\n\n", str);
    }
    How is this an incompatible pointer type?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Because the type expected by your function isn't what a pointer to a 2D array is.

    void printDat( char (*str)[50] );
    would match your call.

    Perhaps you meant to call it with namePtr as the parameter, that should work.

    Mind you, the printf() in the function is wrong as well.
    If you're using gcc as your compiler, add the "-Wall" option.

    Oh, and the trailing "\0" on your string initialisers are pointless, as C will add one of these for you anyway.
    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.

  3. #3
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Thanks Salem, you're always a great help. =)

  4. #4
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    3 Questions:

    Why is the compiler still giving me a warning if the function parameter and function call match now? (When I don't use -Wall)

    In a previous post, you (Salem) told me to include the \0 for strings.

    Quote Originally Posted by Salem View Post
    2. You forgot to add \0 to your strings
    from: http://cboard.cprogramming.com/showthread.php?t=94353
    Why wouldn't it also be necessary to include the terminating character for this program?

    The program is now printing out a bunch of crap (strange characters)
    Here is my updated code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void printDat(char (*)[50]);
    
    int main()
    {
            int i;
    
            char stringPointers[10][50] = {  "Hello!",
                                             "Happy Halloween!",
                                             "How are you?",
                                             "1337",
                                             "Coffee is good.",
                                             "r00t",
                                             "C is efficient.",
                                             "Half-Life 2 rocks.",
                                             "So does Star Wars.",
                                             "programmingSkills == \"$$$\""};
    
            char *namePtr[10];
            for(i = 0; i < 10; i++)
                    namePtr[i] = stringPointers[i];
    
            printDat(namePtr);
    
            return 0;
    }
    
    void printDat(char (*str)[50])
    {
            printf("\n\nstr = &#37;s\n\n", str);
    }
                                                                  33,1          Bot
    Last edited by kwikness; 10-30-2007 at 07:48 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Why is the compiler still giving me a warning if the function parameter and function call match now?
    Because you changed both things, rather than one thing or the other.

    Your OLD function declaration matches your NEW function call, and vice-versa.

    > In a previous post, you (Salem) told me to include the \0 for strings.
    In a previous post, you couldn't count (and got the dimensions the wrong way round).

    char line[10] = "hello";
    This has 5 characters and 5 trailing \0 to fill the array.

    char line[] = "hello";
    This has 5 characters and 1 trailing \0 to fill the array. The compiler works out the size of the array from the number of initialising characters.

    char line[5] = "hello";
    This has 5 characters and NO trailing \0. The result isn't immediately usable by the vast majority of str... functions, but is legal (in C at least) none the less.

    char line[4] = "hello";
    Will throw an error as there are more characters than space allowed for.

    The printf() in the function is still wrong given the type of parameter passed.
    Try
    printf("%s", str[0] );
    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.

  6. #6
    Young C n00b
    Join Date
    Jul 2006
    Posts
    59
    Thank you very much for clarifying that for me. My program works now. Now time to start on the string sorting part of the application.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM