By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,550 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,550 IT Pros & Developers. It's quick & easy.

Passing an array to a function by reference/pointers

P: n/a
Hi!

How to pass an (multidimensional)array of something to a function with
reference/pointer?

Can anyone help me with that?

Thanks,
Ohmu
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Ohmu" <oh***@hot.ee> wrote in message
news:37**************************@posting.google.c om...
Hi!

How to pass an (multidimensional)array of something to a function with
reference/pointer?

Can anyone help me with that?

Thanks,
Ohmu


Here's how to prototype and call functions with arrays, references and
pointers.

// one dimension with pointer
void function(int *a);
int array[10];
function(array);

// two dimensions with pointer
void function(int (*a)[20]);
int array[10][20];
function(array);

// three dimensions with pointer
void function(int (*a)[20][30]);
int array[10][20][30];
function(array);
// one dimension with reference
void function(int (&a)[10]);
int array[10];
function(array);

// two dimensions with reference
void function(int (&a)[10][20]);
int array[10][20];
function(array);

// three dimensions with reference
void function(int (&a)[10][20][30]);
int array[10][20][30];
function(array);

john
Jul 19 '05 #2

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bi************@ID-196037.news.uni-berlin.de>...
Here's how to prototype and call functions with arrays, references and
pointers.

// one dimension with pointer
void function(int *a);
int array[10];
function(array);
<snip> // one dimension with reference
void function(int (&a)[10]);
int array[10];
function(array); <snip>
john


but how to pass the array when the size is read from file, int array[size][size] ??

Ohmu
Jul 19 '05 #3

P: n/a

"Ohmu" <oh***@hot.ee> wrote in message
news:37**************************@posting.google.c om...
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bi************@ID-196037.news.uni-berlin.de>...
Here's how to prototype and call functions with arrays, references and
pointers.

// one dimension with pointer
void function(int *a);
int array[10];
function(array);

<snip>
// one dimension with reference
void function(int (&a)[10]);
int array[10];
function(array);

<snip>

john


but how to pass the array when the size is read from file, int

array[size][size] ??
Ohmu


int array[size][size]; is not a legal array declaration.

If your 2d array is dynamic then you need to allocate some memory for it and
use pointers. E.g.

void function(int** a);

int **array;
array = new int*[size];
for (int i = 0; i < size; ++i)
array[i] = new int[size];
function(array);

This question is in the FAQ

http://www.parashift.com/c++-faq-lit...html#faq-16.15

john
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.