Thread: Warning about incompatible pointer types

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Canada
    Posts
    3

    Warning about incompatible pointer types

    Greetings everyone.

    I'm a little new to programming in C, just programming for fun. Right now I'm working on a text based adventure game done with the curses library. One of the things I want to do is have an inventory system where the user can collect items and use them later. To store the list of items in inventory, I declared an array of character pointers like so:

    Code:
    char *list[5] = {NULL, NULL, NULL, NULL, NULL};
    Then, later on, when the user types something like "take wallet" I have code that analyzes the string and, if the wallet hasn't already been taken, stores "wallet" in list[0]. That code looks like this:

    Code:
    if (strcmp(&list[0], "wallet") != 0) {
    	list[0] = "wallet";
    	printw("You have taken the wallet.\n");
    	home0(); //this function is where the wallet is found
    }
    else {
    	printw("You already have the wallet.\n");
    	home0();
    }
    Whenever it compiles, I get a warning about strcmp, which says "passing argument 1 of 'strcpy' from incompatible pointer type." I read somewhere that all warnings should be fixed. When I run the program though, everything works as expected. If that is the case, should I really be worried about this warning? And if so, what exactly is it that I'm doing wrong?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    &list[0] is of type "pointer to pointer to char". strcmp() expects a (const) pointer to char. A "pointer to char" and "pointer to pointer to char" are incompatible pointer types, because they point at different types of things.

    It is also a really bad idea to pass NULL to strcmp(), as either argument. Doing so causes strcmp() to exhibit undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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
    Well the first test should be
    Code:
    if ( list[0] != NULL )
    This tells you whether there is anything at all in the first slot.

    Then you check to see if it is a wallet (or not)
    Code:
    if ( strcmp( list[0], "wallet" ) == 0 ) {
      // it's a wallet
    }
    > list[0] = "wallet";
    The code you have at the moment is suggesting that you drop whatever you previously had, and pick up a wallet.
    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
    Join Date
    Oct 2011
    Location
    Canada
    Posts
    3
    Thanks grumpy. I'm going to look now for a way to improve the code. I forgot that arrays are essentially pointers. I also didn't know that passing NULL to strcmp() was undefined.

    Also, thanks for the suggestion Salem. I imagine that will be easier as the program gets developed, so that I'm not constantly assigning/reassigning NULL to items that are used or collected again. That could also be used to have a smaller array (ie, limit the number of items the user can carry at one time.)
    Last edited by ajrmoore; 10-20-2011 at 01:26 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ajrmoore View Post
    I forgot that arrays are essentially pointers.
    Be very careful when applying that observation. It is useful sometimes, and wrong other times.

    Arrays and pointers are very different things. They are equivalent in some contexts (for example, the name of an array is converted by the compiler into a pointer when passing it as an argument to a function) but not in others (for example, it is possible to have a NULL pointer but not possible to have a NULL array).

    I do suggest you spend some time building up an understanding of pointers and arrays. It is important to understand when they can be treated as equivalent, and when they cannot.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Time for the OP to swing by here -> Arrays and Pointers
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How would I fix this incompatible pointer type warning
    By ogglock in forum C Programming
    Replies: 2
    Last Post: 07-11-2011, 01:17 AM
  2. incompatible pointer types
    By quasigreat in forum C Programming
    Replies: 4
    Last Post: 05-22-2008, 12:30 AM
  3. Incompatible Pointer Type Warning
    By kwikness in forum C Programming
    Replies: 5
    Last Post: 10-30-2007, 06:14 PM
  4. incompatible pointer types
    By mart_man00 in forum C Programming
    Replies: 3
    Last Post: 04-20-2003, 08:32 PM