Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > creating multidimensional array at runtime and passing it as parameter to a function

Reply
Thread Tools

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

 
 
nitinm
Guest
Posts: n/a
 
      09-14-2007
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

 
Reply With Quote
 
 
 
 
Punkie
Guest
Posts: n/a
 
      09-14-2007
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)


 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      09-14-2007
On Fri, 14 Sep 2007 03:40:18 -0000, nitinm
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
Multidimensional array, passing to a function Michael C Programming 16 11-30-2009 08:07 AM
Passing multidimensional array as function argument Felipe Ribeiro C Programming 4 05-13-2009 10:18 AM
Creating and accessing a JavaScript multidimensional array? Keiron Waites Javascript 5 10-22-2003 08:39 PM
multidimensional arrays as function parameter Salmen C Programming 3 09-11-2003 03:01 PM



Advertisments