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

N

nitinm

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?
 
P

Punkie

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)
 
B

Barry Schwarz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top