Passing pointers to two-dimensional arrays of structs

This is a discussion on Passing pointers to two-dimensional arrays of structs within the C Programming forums, part of the General Programming Boards category; Hi, I have written a function which receives a pointer to a two-dimensional array of structs. To use the GNU ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    2

    Passing pointers to two-dimensional arrays of structs

    Hi,

    I have written a function which receives a pointer to a two-dimensional array of structs. To use the GNU Scientific Library (which is excellent) I need to wrap the function in another function of form:

    void wrapper(void *)

    How should I do this? The short but complete example below works but my compiler complains about an incompatible type:

    main.c:18: warning: passing argument 1 of 'func' from incompatible pointer type
    [refering to the func(z) line in wrapper()]

    I think the problem is that "struct coord ** z" and "struct coord z[][]" are not of the same type. What is the right way to do this? The one-dimensional version compiles silently and works perfectly. All comments welcome!

    Thanks,
    Neil.

    Code:
    #include <stdio.h>  
    
    struct coord {
      int x;
      int y;
    };
      
    void func(struct coord z[2][2])
    {
      printf("%d,%d\n", z[0][0].x, z[0][0].y);
    }
     
    void wrapper(void * s)
    {
        struct coord ** z;
        
        z = (struct coord **) s;
        func(z);
    }
     
    int main(void)
    {
      struct coord b[2][2];
      void * c;
        
      b[0][0].x = 1;
      b[0][0].y = 2;
      b[0][1].x = 3;
      b[0][1].y = 4;
      b[1][0].x = 5;
      b[1][0].y = 6;
      b[1][1].x = 7;
      b[1][1].y = 8;
      
      func(b);
    
      c = (void *) b;
      wrapper(c);
    
      return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A pointer to a pointer is not the same as a pointer to an array.
    http://c-faq.com/aryptr/pass2dary.html - or thereabouts
    Code:
    #include <stdio.h>  
    
    struct coord {
      int x;
      int y;
    };
      
    void func(struct coord z[2][2])
    {
      printf("%d,%d\n", z[0][0].x, z[0][0].y);
    }
     
    void wrapper(void * s)
    {
        struct coord (*z)[2] = s;
        func(z);
    }
     
    int main(void)
    {
      struct coord b[2][2] = { { {1,2}, {3,4} }, { {5,6}, {7,8} } };
      func(b);
      wrapper(b);
      return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    2
    Thank you Dave. Completely fixed - really helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  2. passing arrays of structs :(
    By Tbrez in forum C Programming
    Replies: 16
    Last Post: 04-05-2009, 01:52 AM
  3. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 05:48 PM
  4. structs like pointers?
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 03-14-2003, 02:16 AM
  5. Pointers and Arrays
    By C Seņor in forum C Programming
    Replies: 4
    Last Post: 06-16-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21