Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing an array to a function by reference/pointers

Reply
Thread Tools

Passing an array to a function by reference/pointers

 
 
Ohmu
Guest
Posts: n/a
 
      08-30-2003
Hi!

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

Can anyone help me with that?

Thanks,
Ohmu
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-30-2003

"Ohmu" <> wrote in message
news: 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


 
Reply With Quote
 
 
 
 
Ohmu
Guest
Posts: n/a
 
      08-30-2003
"John Harrison" <> wrote in message news:<biplo1$bqaq9$>...
> 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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-30-2003

"Ohmu" <> wrote in message
news: om...
> "John Harrison" <> wrote in message

news:<biplo1$bqaq9$>...
> > 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


 
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 an array from FORTRAN to C then passing it within C andReturning it to FORTRAN deadpickle C Programming 1 11-07-2010 02:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM
Passing a contiguously allocated 2D array to a function expecting a 1 dimesnional array Jonathan Underwood C Programming 2 08-13-2003 04:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57