Thread: Incompatible pointer types - C

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    135

    Incompatible pointer types - C

    Hey.

    Question regarding the call in the main to checkSizesLast function -
    The compiler does warns about icompatible pointer types, but I don't succeed to adjust it...

    I thought it would be correct to send to checkSizesLast the matrix de-referenced like *matrix but it doesn't work too.

    What's the matter here?

    Thanks.

    Code:
    #include <stdio.h>
    
    
    void checkSizes(char matrix3d[10][20][30])
    {
        printf ("%lu\n", sizeof(matrix3d));
        printf ("%lu\n", sizeof(*matrix3d));
        printf ("%lu\n", sizeof(**matrix3d));
        printf ("%lu\n", sizeof(***matrix3d));
    }
    
    
    void checkSizesAgain(char (*matrix3d)[20][30])
    {
        printf ("%lu\n", sizeof(matrix3d));
        printf ("%lu\n", sizeof(*matrix3d));
        printf ("%lu\n", sizeof(**matrix3d));
        printf ("%lu\n", sizeof(***matrix3d));
    }
    
    
    void checkSizesLast(char* matrix3d[20][30])
    {
        printf ("%lu\n", sizeof(matrix3d));
        printf ("%lu\n", sizeof(*matrix3d));
        printf ("%lu\n", sizeof(**matrix3d));
        printf ("%lu\n", sizeof(***matrix3d));
    }
    
    
    int main()
    {
        char matrix[10][20][30];
        char matrix2[1][20][30];
        checkSizes(matrix);
        checkSizesAgain(matrix);
        checkSizesLast(matrix);
        checkSizes(matrix2);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Nothing you have in main at the moment can correctly call checkSizesLast() with any amount of casting or dereferencing.

    checkSizesLast is expecting an array (of some dimensions) who's most basic elements are char pointers.

    You can't make an array of pointers-to-T out of an array of T just by casting.

    Code:
    int main()
    {
        char matrix[10][20][30];
        char matrix2[1][20][30];
        char *m3[20][30];
        checkSizes(matrix);
        checkSizesAgain(matrix);
        checkSizesLast(m3);
        checkSizes(matrix2);
        return 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.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @Salem
    And this is a question from some exam in the past.
    Thank you!

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Do you understand that these all say essentially the same thing?
    Code:
    void f(char   matrix3d [10][20][30]);
    void g(char   matrix3d [  ][20][30]);
    void h(char (*matrix3d)    [20][30]);
    When you pass an array to a function, the outermost dimension "decays" into a pointer, losing the size information for that dimension.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @john.c
    Yea yea I know that - thank you for the highlight - appreciate it.

    BTW - is the third one equivalent to the first two statements?

    EDIT:
    You have said they are the same thing.
    Umm... but to me it seems like the third one sends a pointer to an array of matrix 20X30 of chars.... It looks like there is no reason to decay anything here as matrix3d is already decayed in some manner
    Last edited by HelpMeC; 12-05-2019 at 01:04 PM.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Yes, they are all the same. The point is that you can pass the exact same array to all of them. The last version is just explicitly saying what is actually happening. The other two are in a sense somewhat misleading, especially the first, which seems to suggest that the 10 has some meaning whereas it has no meaning at all. It is completely ignored.
    Code:
    #include <stdio.h>
     
    void f(char   m [10][20][30]) { printf("%c\n", m[0][0][0]); }
    void g(char   m [  ][20][30]) { printf("%c\n", m[0][0][0]); }
    void h(char (*m)    [20][30]) { printf("%c\n", m[0][0][0]); }
     
    int main() {
        char m[10][20][30];
        m[0][0][0] = 'x';
        f(m);
        g(m);
        h(m);   // no complaints !
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    @john.c
    Very helpful - thank you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault and incompatible pointer types
    By Mr.Kumar in forum C Programming
    Replies: 3
    Last Post: 02-27-2016, 02:04 AM
  2. Warning about incompatible pointer types
    By ajrmoore in forum C Programming
    Replies: 5
    Last Post: 10-20-2011, 06:03 AM
  3. assignment from incompatible pointer types
    By iunah in forum C Programming
    Replies: 13
    Last Post: 10-12-2008, 03:32 AM
  4. incompatible pointer types
    By quasigreat in forum C Programming
    Replies: 4
    Last Post: 05-22-2008, 12:30 AM
  5. incompatible pointer types
    By mart_man00 in forum C Programming
    Replies: 3
    Last Post: 04-20-2003, 08:32 PM

Tags for this Thread