Thread: 3 d array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    47

    3 d array

    Function that takes as input a 3 dimensional array, and its dimensions

    How can I do that

    Code:
    void inputArray(int fd,int sd,int td,int array[fd][sd][td])

  2. #2
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    Quote Originally Posted by lio View Post
    Function that takes as input a 3 dimensional array, and its dimensions

    How can I do that

    Code:
    void inputArray(int fd,int sd,int td,int array[fd][sd][td])
    i don't see any question here. please be specific

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    If I declare the function like that ,it gives me error that the variables are not declared

  4. #4
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    declare array size as a constant and try to post the full code if possible

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    I am trying to define function that takes as input a 3 dimensional array, and its dimensions
    Code:
    #include <stdio.h>
    #define fd 5
    #define sd 5
    #define td 5
    void inputArray(int fd,int sd,int td,int array[fd][sd][td]);
    int main()
    {
        
        getchar();
    	return 0;
    }
    
    void inputArray(int fd,int sd,int td,int array[fd][sd][td])
    {
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to name your integer parameters and your array sizes differently.

    Your code expands to something like
    void inputArray(int 5,int 5,int 5,int array[5][5][5]);
    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.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    Thanks
    I am trying to pass to the function array defined by the user ,how can I do that

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you have
    int array[10][20][30];

    Then the prototype/definition of the function would look like this
    void func ( int array[10][20][30] );

    It might also look like this, without changing anything else.
    void func ( int array[][20][30] );
    void func ( int (*array)[20][30] );


    Since it is only the left-most dimension which can vary (all the others have a fixed size for a given multi-dimensional array), you only need pass the length of the array in it's left-most dimension.

    So perhaps it becomes
    void func ( int array[10][20][30], size_t len );

    Calling this function is easy
    func( array, 10 );
    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.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Or you can just pass a pointer, hoping that the user has defined or allocated the 3d array correctly...
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    47
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM