473,544 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing an array to a function by reference/pointers

Hi!

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

Can anyone help me with that?

Thanks,
Ohmu
Jul 19 '05 #1
3 119611

"Ohmu" <oh***@hot.ee > wrote in message
news:37******** *************** ***@posting.goo gle.com...
Hi!

How to pass an (multidimension al)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
"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

"Ohmu" <oh***@hot.ee > wrote in message
news:37******** *************** ***@posting.goo gle.com...
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10058
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
9
4770
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not...
8
4101
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
2
4296
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element pointer. :-)) You were right about by mixed code: quickly writing an example to clarify the matter I made a C++ program, sorry for the inconvenience....
4
2488
by: hello smith | last post by:
I have a lot of functions that add values to an array. They alos update a global variable of type int. Currently, I use a global variable to hold this array. All functions access this array global variable and add their values. Then they increment the global int variable. Is it faster to pass the array and the int variable by reference...
11
8098
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub...
7
3292
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class...
2
4415
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
8
3488
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
7449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7642
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5950
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5316
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3432
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1861
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.