Thread: Passing array of arbitrary dimension

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    19

    Passing array of arbitrary dimension

    Say I want to create a function which finds the highest number in an array of arbitrary dimension, how would I construct the parameters to receive such a variable?

    I don't want to create max functions for each of the following:

    Code:
    double max(double *num, int length) {...}
    double max2d(double **num,  int length) {...}
    double max3d(double ***num, int length) {...}
    etc...

    That would be painful. I'd rather have a universal function that takes an array of any dimension, along with the number of the dimension itself.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, you could use a little trickery. Make a function that takes a pointer and the number of dimensions. Then cast it to correct type inside function. Be sure to cast the pointer to a single pointer when passing.
    I don't know of any other clean way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No need to cast, just use pointer arithmetic. Assuming of course each dimension has the same number of elements.
    Last edited by zacs7; 04-03-2008 at 06:54 PM.

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I'm not completely sure this will work correctly as its not something I have done before, but could you not cast the pointer type to a 1d array.
    Code:
    #include <stdio.h>
    void foo(int *array, int dimensions)
    {
    	printf("&#37;u\n", dimensions);
    }
    
    int main()
    {
    	int a[100];
    	int b[10][10];
    	foo((int*)a, 1);
    	foo((int*)b, 2);
    	return 0;
    }
    This shoudl result in an array that is effectivly flattened within your function so if your dimensions vary in size you may have to handle this somehow.

    Edit:
    No need to cast, just use pointer arithmetic.
    Maybe am missing something here but it wont compile for me unles I cast it.
    Last edited by mike_g; 04-03-2008 at 06:53 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I'd rather have a universal function that takes an array of any dimension, along with the number of the dimension itself.
    Perhaps a void *?
    Code:
    double max(void *num, int num_dimensions, int length) {...}

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by mike_g View Post
    Maybe am missing something here but it wont compile for me unles I cast it.
    Pass a pointer to the array. It doesn't matter how many dimensions it has, arrays are contiguous.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unless it's dynamically allocated, which could pose a problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, but he didn't say it was, still the same thing really -- just follow the pointers.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Pass a pointer to the array. It doesn't matter how many dimensions it has, arrays are contiguous.
    Well I know that arrays are contiguous On second inspection it was only a warning that gcc gave, but if I remember correctly VS wont compile unless you cast it, and tbh I'd rather not see any unnecessary warnings when i compile. Makes the real problems harder to find.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by mike_g View Post
    ...but if I remember correctly VS wont compile unless you cast it, and tbh I'd rather not see any unnecessary warnings when i compile. Makes the real problems harder to find.
    That's because it's bad if it compiles.

    And I do get the feeling it's dynamic looking at the function prototypes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    #include <stdio.h>
    
    double max(double * start, size_t length);
    
    int main(void)
    {
        double a[] = {0.5, 20.4, 158.1};
        
        double b[][3] = {
                            {0.5, 20.4, 158.1},
                            {0.785, 1450.4, 1258.1},
                            {0.9, 254.68, 14.1}
                        };
                        
        double c[][3][3] =  {   
                                {
                                    {0.5, 360.4, 4758.1},
                                    {0.4755, 2047.4, 1548.1},
                                    {0.5, 270.44, 158.145}
                                },
                                
                                {
                                    {47.5, 2470.4, 15478.1},
                                    {470.5, 2440.4, 15847.1},
                                    {0.75, 2470.47, 14758.1}
                                }
                            };
        
        printf("max of a = &#37;f\n", max(&a[0], 3));
        printf("max of b = %f\n", max(&b[0][0], 9));
        printf("max of c = %f\n", max(&c[0][0][0], 18));
        return 0;
    }
    
    double max(double * start, size_t length)
    {
        double m = start[0];
        size_t i = 0;
        
        for(i = 0; i < length; i++)
        {
            if(start[i] > m)
                m = start[i];
        }
        
        return m;
    }
    Ofcourse if it's DMA'd then you'd need a more robust solution
    Last edited by zacs7; 04-03-2008 at 07:12 PM.

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I never expected it to be dynamically assigned. Would cause too much problems for something like this. But it is a possibility, youre right.

    As for using a void pointer, that might be good, but it dosent say anythin about the data type. Personally I never used void pointers myself yet, maybe I should look into it.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Perhaps try map each pointer to a dimension (with how many elements it contains and whatever). Pass the map as an array (one map per dimension).

    I don't know if that's the best way to do it
    Last edited by zacs7; 04-03-2008 at 07:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM