364,036 Members | 5201 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Passing an array to a function by reference/pointers

Ohmu
P: n/a
Ohmu
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


John Harrison
P: n/a
John Harrison

"Ohmu" <ohmar@hot.ee> wrote in message
news:37c0fd9c.0308292353.33ee6b6e@posting.google.c om...[color=blue]
> Hi!
>
> How to pass an (multidimensional)array of something to a function with
> reference/pointer?
>
> Can anyone help me with that?
>
> Thanks,
> Ohmu[/color]

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

Ohmu
P: n/a
Ohmu
"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<biplo1$bqaq9$1@ID-196037.news.uni-berlin.de>...[color=blue]
> 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);
>[/color]
<snip>[color=blue]
> // one dimension with reference
> void function(int (&a)[10]);
> int array[10];
> function(array);[/color]
<snip>[color=blue]
>
> john[/color]

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

Ohmu
Jul 19 '05 #3

John Harrison
P: n/a
John Harrison

"Ohmu" <ohmar@hot.ee> wrote in message
news:37c0fd9c.0308300447.1963ea4d@posting.google.c om...[color=blue]
> "John Harrison" <john_andronicus@hotmail.com> wrote in message[/color]
news:<biplo1$bqaq9$1@ID-196037.news.uni-berlin.de>...[color=blue][color=green]
> > 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);
> >[/color]
> <snip>[color=green]
> > // one dimension with reference
> > void function(int (&a)[10]);
> > int array[10];
> > function(array);[/color]
> <snip>[color=green]
> >
> > john[/color]
>
> but how to pass the array when the size is read from file, int[/color]
array[size][size] ??[color=blue]
>
> Ohmu[/color]

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

Post your reply

Help answer this question



Didn't find the answer to your C / C++ question?

You can also browse similar questions: C / C++