Thread: Passing PART of a 3D array

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    Passing PART of a 3D array

    Is there any way to pass only two dimensions of a 3D array.

    for arr[2][3][4], lets say I want to use [3] and [4] to calculate something, and i only need those dimensions. Say i want to pass it to a function called calculate. I would try this...but it isn't working...

    Code:
    float calculate(float arr[3][4]);
    
    ...
    ...
    
    printf("We have found %f", calculate(arr[2]);

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Pass the whole array... but only use the sections you need.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    My profess. requires that we only pass portions of the array that are necessary for that particular task.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your professor obviously isn't a programmer... You can't split up arrays like that.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not splitting up an array. This is a "slice", and in some languages it's supported - like Go! uses them alot.

    In C, I wouldn't do it, but you could work it out using pointers I believe, (which is what the array passing degrades to anyway in C). Seems error prone, however.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by scatterbrain View Post
    Is there any way to pass only two dimensions of a 3D array.

    for arr[2][3][4], lets say I want to use [3] and [4] to calculate something, and i only need those dimensions. Say i want to pass it to a function called calculate. I would try this...but it isn't working...

    Code:
    float calculate(float arr[3][4]);
    
    ...
    ...
    
    printf("We have found %f", calculate(arr[2]);
    This line below is wrong; because you only have two legal values for first dimension "0" and "1".

    Code:
    printf("We have found %f", calculate(arr[2]);
    I agree with adak "Seems error prone ...".

    You might try passing "&arr[0]" or "&arr[1]" instead of arr[2].

    Tim S.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    Thanks for your help Adak and stahta01, I'll try that out.

    CommonTater, thanks for that typical condescending forum-programmer response. That's unnecessary and not helpful at all, obviously my programming teacher is a programmer. This is only a minor example here, but someone please explain to me why people on programming message boards are so ........ing arrogant and unhelpful at times.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Make sure you're completely clear about your instructions (considering an assignment like this does not seem reasonable for any class in C). I would think it easier to "pass two dimensions" if you just passed one page of the array (this is still a third dimension, just a cross-section of it) rather than the third column of four pages. I think it would be error-prone as well, but perhaps less error-prone than what you wanted.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by stahta01 View Post
    You might try passing "&arr[0]" or "&arr[1]" instead of arr[2].
    Tim S.
    stahta01 is correct. you pass in the address of the matrix you are concerned with, which is one element of the first index. then the calculate function should have type matching the type of the 2d matrix, which you have correct.
    [CODE]

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm just guessing that Tater was looking at it from a production programmer point of view, and in C, I doubt if you it would be wise to do that - because again, we're just talking about a pointer being passed, no matter how large the array is.

    Your prof., on the other hand, is probably giving you this assignment as a tool to learn and practice how to work with the innards of an array. His point is an educational one. Do you have some class notes on this?

  11. #11
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    posting 2 threads for the same prob!!!????

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    There's nothing wrong with passing around array slices.
    Code:
    #include <stdio.h>
    
    void foo ( int arr[3][4] ) {
        arr[0][0] = 1;
    }
    int main ( ) {
        int arr[4][3][4] = { { { 0 } } };
        foo(arr[0]);
        foo(arr[1]);
        printf("%d %d\n", arr[0][0][0], arr[1][0][0] );
        return 0;
    }
    I mean, you're doing the same thing if you're trying to tokenise a string, and passing around &myString[offset] to some other function accepting a char*.
    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.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Code:
    #include <stdio.h>
    
    #define ARRAY_MEMS(xx_a) (sizeof(xx_a)/sizeof(xx_a[0]))
    
    void
    print_one(int idx, float a[][9], int x,int y)
    {
       int j,k;
       printf("idx = %6d\n", idx);
       for(j=0; j<x; j++)
       {
          for(k=0; k<y; k++)
          {
             printf("%7.1f ", a[j][k]);
          }
          printf("\n");
       }
    }
    
    int
    main()
    {
       float a[5][7][9] ;
       int i,j,k;
       for(i=0; i<ARRAY_MEMS(a); i++)
       {
          for(j=0; j<ARRAY_MEMS(a[i]); j++)
          {
             for(k=0; k<ARRAY_MEMS(a[i][j]); k++)
             {
                a[i][j][k] = (i * 10000) + (j * 100) + k;
             }
          }
       }
       for(i=0; i<ARRAY_MEMS(a); i++)
       {
          print_one(i,a[i], ARRAY_MEMS(a[i]), ARRAY_MEMS(a[i][0]));
          printf("\n\n");
       }
       return(0);
    
    }
    Last edited by sparkomemphis; 12-22-2011 at 01:03 PM.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CommonTater View Post
    Your professor obviously isn't a programmer... You can't split up arrays like that.
    Yes you can. The code as posted should be working fine. Can we see real code, not snippets?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-17-2011, 06:21 AM
  2. A problem in part of array.
    By dirtydrummer in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 02:04 AM
  3. Passing as argument a part of a table
    By myle in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:00 AM
  4. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 01:38 AM
  5. Referencing part of an array.
    By Russell in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2005, 03:28 PM