Thread: Sending a pointer to multi-dim array to a function

  1. #1
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210

    Question Sending a pointer to multi-dim array to a function

    Is it wrong to do this:

    Code:
    int data[20][30];
    int *dPtr = data[0];
    
    myFunc( dPtr, 20, 30 );
    I want to send a multi-dim array to a function, but I want to have the function accept an array of any size. The sizes of each dimension are sent as arguments though, so I'm not flying blind.

    I mean, I know this works, but I just want to know if I would be shot in a commercial environment for doing it?

    And if it is evil, how else can I send multi-dim arrays to functions without explicitly specifying their length? Are VLAs the only way?
    Last edited by bivhitscar; 06-08-2006 at 01:18 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  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
    What you're basically doing is this
    http://c-faq.com/aryptr/ary2dfunc2.html

    An alternative is
    Code:
    myFunc( int**, int, int );
    int data[20][30];
    int *dPtr[20];
    for ( i = 0 ; i < 20 ; i++ ) dPtr[i] = data[i];
    myFunc( dPtr, 20, 30 );
    But this has it's own attendant ugliness as well.

    > but I just want to know if I would be shot in a commercial environment for doing it?
    That depends on who you work for, and how good an argument you can present for doing it this way as opposed to any alternative.

    My next question would be what is the variability of your array sizes?
    a[10][10] to a[20][20] perhaps?
    Larger? smaller?
    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
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Wow, that link you gave is exactly the solution I came up with.

    Anyway, the arrays are simulating pixels in an image, so they could stretch out to a[1000][1000] or more depending on the image size. Why do you ask?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Depending on the situation, sometimes it's easier to just a single array as opposed to a matrix.

    Code:
    int data[20][30];
    //could be
    int data[20*30];

  5. #5
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Oh yeh, I'll keep that in mind. The current problem spcifically defines the use of multi-dim arrays, but that solution looks pretty cool.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by bivhitscar
    Is it wrong to do this:
    And if it is evil, how else can I send multi-dim arrays to functions without explicitly specifying their length? Are VLAs the only way?
    Wrap it around a structure and have a void* pointer to whatever data you will need in the calling function along with the dimensions of the data, inside the strucure.

    Example:

    Code:
    struct GenericDataPointer
    {
        int x, y; // .. array dimensions
        void* pdata; // .. pointer to array of any size or dimension or anything else your heart desires.
    };

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    More structure abuse!
    Code:
    struct abuse
    {
        size_t x, y;
        int data[];
    };
    ...
    struct abuse *foo = malloc( sizeof( struct abuse ) + ( sizeof( int ) * (getx() * gety()) ) );
    I think that looks about right. I never use these things.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Structs can actually be useful in these situations. You can create functions which take pointers to these structs, and do operations with the data.

    Like

    Code:
    struct matrix
    {
        size_t xsize, ysize;
        int *data;
    }
    
    void allocatenewmatrix(struct matrix *m);
    void deletematrixmemory(struct matrix *m);
    int foobar(struct matrix *m);
    char *blow_job(struct matrix *m, char *input);
    /* etc */
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean kinda like the post 2 up from yours says? Oh, and the one right above yours also.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    quzah, can you please explain size_t to me. I've seen it a lot and I've tried searching for a good explanation, but I still don't get it.

    And thanks for the struct idea people, I didn't think of that.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    size_t is an ANSI C standard data type of unsigned composition. It is safe to use for indexing arrays, because provided you don't run off the end, you'll never 'under read' your array. linky-link


    Quzah.
    Last edited by quzah; 06-09-2006 at 12:15 AM. Reason: linky-link
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM