Thread: Incompatible types when assigning to type ‘char[11]’ from type ‘char *'.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    69

    Incompatible types when assigning to type ‘char[11]’ from type ‘char *'.

    Code:
    void table_sorting (char l_plates[M][N], double payments[M]) {
        int  i, j;
        char *tmp1;
        double tmp2;
        
        
        for (i = 0; i < M; i++) {
            for (j = i; j < M; j++) {
                if (strcmp(l_plates[i], l_plates[j]) > 0) {
                    
                    tmp1 = l_plates[i];
                    l_plates[i] = l_plates[j];
                    l_plates[j] = tmp1;
                    
                    tmp2 = payments[i];
                    payments[i] = payments[j];
                    payments[j] = tmp2;
                }
            }
        }
    }
    So at lines 12 & 13 I'm getting
    error: incompatible types when assigning to type ‘char[11]’ from type ‘char *'

    How come?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    l_plates[i] = tmp1 is trying to change the location of the array at l_plates[i] (i.e. the array of chars that forms that license plate). You can't move where arrays or elements of arrays are, meaning you can't swap strings in the same way you might with integers. In general, you can't assign strings or arrays with the = sign. You have to use something like memcpy or strcpy to copy/swap arrays or strings. Also, you need to allocate space for tmp1, since right now it's just a pointer to nowhere in particular. It doesn't point to valid space to copy to. Try:
    Code:
    char tmp1[N];  // enough space to hold any of the license plate strings
    ...
    strcpy(tmp1, l_plates[i]);
    strcpy(l_plates[i], l_plates[j]);
    strcpy(l_plates[j], tmp1);

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    Thank you, you're totally right! Looks like I need to get more familiar with pointers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 03-06-2008, 02:32 PM
  2. type char vs type int
    By stanlvw in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 11:04 AM
  3. Converting type string to type const char*
    By rusty0412 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 05:59 PM
  4. Converting from type char to const char*
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2003, 09:51 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM