creating multidimensional array at runtime and passing it as parameter to a function

Discussion in 'C Programming' started by nitinm, Sep 14, 2007.

  1. nitinm

    nitinm Guest

    hi
    I want to make a program whose requirement are as following:

    1) it has to create an NxN matrix after reading input (i.e. N) from a
    file in the main() itself. [so runtime array initialization]
    2) it has to send the array as a parameter to a function. [for
    recursive calls]

    condition 1 is not a problem when i use gmalloc for array
    initialization. but for condition 2, i require that the function that
    is called by the main(), it has to specify one of the
    dimensions(because it is a multidimensional array) like function(int
    array[][N])


    any ideas?

    --
    Nitin Munjal
     
    nitinm, Sep 14, 2007
    #1
    1. Advertising

  2. nitinm

    Punkie Guest

    Use a pointer to pass the variable. Make N a global var or pass the size as
    an argument.
    void function(int* arr, int size)
     
    Punkie, Sep 14, 2007
    #2
    1. Advertising

  3. On Fri, 14 Sep 2007 03:40:18 -0000, nitinm
    <> wrote:

    >hi
    > I want to make a program whose requirement are as following:
    >
    >1) it has to create an NxN matrix after reading input (i.e. N) from a
    >file in the main() itself. [so runtime array initialization]
    >2) it has to send the array as a parameter to a function. [for
    >recursive calls]
    >
    >condition 1 is not a problem when i use gmalloc for array


    What is gmalloc?

    >initialization. but for condition 2, i require that the function that
    >is called by the main(), it has to specify one of the
    >dimensions(because it is a multidimensional array) like function(int
    >array[][N])


    Look at the faq (c-faq.com). It shows a method of defining a 2D array
    using a pointer to pointer. If you use this, you don't need to
    (actually cannot) specify the dimension as part of the array parameter
    but you can (should) pass the dimension in a second integer argument.


    Remove del for email
     
    Barry Schwarz, Sep 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Salmen
    Replies:
    3
    Views:
    430
    Tim Hagan
    Sep 11, 2003
  2. Felipe Ribeiro

    Passing multidimensional array as function argument

    Felipe Ribeiro, May 12, 2009, in forum: C Programming
    Replies:
    4
    Views:
    960
    Felipe Ribeiro
    May 13, 2009
  3. Michael
    Replies:
    16
    Views:
    1,015
    David Thompson
    Nov 30, 2009
  4. Mister B
    Replies:
    8
    Views:
    558
    Nick Keighley
    Aug 26, 2010
  5. Keiron Waites
    Replies:
    5
    Views:
    128
    Kent Feiler
    Oct 22, 2003
Loading...

Share This Page