Hi. I want to pass an array through a function. how do i do this?
eg.
void SomeFunction(){
int Array[3];
Array[0] = 1;
Array[1] = 2;
Array[2] = 3;
SomeOtherRandomFunction(/*pass the array here*/);
}
void SomeOtherRandomFunction(/*what do i put here*/);
Hamish Dean, PhD 5 2333
"Hamish Dean" <h.****@xtra.co.nz> wrote in message
news:Y6**********************@news.xtra.co.nz... Hi. I want to pass an array through a function. how do i do this?
eg.
void SomeFunction(){ int Array[3];
Array[0] = 1; Array[1] = 2; Array[2] = 3;
SomeOtherRandomFunction(/*pass the array here*/);
SomeOtherRandomFunction (Array);
}
void SomeOtherRandomFunction (int *Array) void SomeOtherRandomFunction(/*what do i put here*/);
HTH
void MyFunc( int *pInt, size_t size ); void SomeFunction(){ int Array[3];
Array[0] = 1; Array[1] = 2; Array[2] = 3;
// SomeOtherRandomFunction(/*pass the array here*/);
MyFunc( Array, sizeof(Array)/sizeof(Array[0]) );
}
void SomeOtherRandomFunction(/*what do i put here*/);
void MyFunc( int *pInt, size_t size )
{
for ( int i=0; i<size; ++i ) {
cout << *pInt++ << endl;
}
}
HTH
Rainer
Big Brian wrote: Everybody seems to forget that C++ does support multidimensional arrays in the language. Everybody imediately suggests using the STL containers, but sometimes this isn't acceptable, and you need to use the built in arrays.
Your code suggests that you fail to recognize some of the issues, and
that you may not understand exactly how arrays and functions work
together. It also demonstrates a problem that could be solved with
std::vector. Consider this example....
#include <iostream>
void f( int array[5][3] )
Mmmmm... magic numbers. Even worse, one of them is completely
meaningless. This is equivalent to
void f( int array[][3] )
or
void f( int (*array)[3] )
Since the 5 does not provide useful information, your function does not
know how many rows this 2-d array contains.
{ for(int i = 0; i < 5; i++ )
Magic number... 5, eh? And what if there are not 5 rows? What if there
are 23? or 2? The only way to do this correctly would be to pass in the
number of rows as a separate argument.
{ for(int j = 0; j < 3; j++ )
Another magic number... but this time it could be replaced with
sizeof(*array) / sizeof((*array)[0])
Or something similar.
{ std::cout << "array[" << i << "," << j << "] =" << array[j][i]
Hmmm... what have we here? i goes from 0 to 4... but your second index
only goes from 0 to 2. You are going outside your array, causing
undefined behavior. This could have been caught if you had used
std::vector with the at() function.
<< std::endl; } } }
int main(int argc , char * argv[]) { int a[5][3] = { {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
a[0][0] = 1; a[2][4] = 4;
Again you've swapped indexes and gone outside the bounds of your array. f(a);
return 0; }
The final and most obvious problem with your code is that your function
is completely incapable of dealing with 2-d arrays that have a different
number of columns.
std::vector is usually a better solution, as it appears it would have
been in this case.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
> > Everybody seems to forget that C++ does support multidimensional arrays in the language. Everybody imediately suggests using the STL containers, but sometimes this isn't acceptable, and you need to use the built in arrays.
Your code suggests that you fail to recognize some of the issues, and that you may not understand exactly how arrays and functions work together. It also demonstrates a problem that could be solved with std::vector.
Consider this example....
#include <iostream>
void f( int array[5][3] )
Mmmmm... magic numbers. Even worse, one of them is completely meaningless. This is equivalent to
void f( int array[][3] )
or
void f( int (*array)[3] )
Since the 5 does not provide useful information, your function does not know how many rows this 2-d array contains.
{ for(int i = 0; i < 5; i++ )
Magic number... 5, eh? And what if there are not 5 rows? What if there are 23? or 2? The only way to do this correctly would be to pass in the number of rows as a separate argument.
{ for(int j = 0; j < 3; j++ )
Another magic number... but this time it could be replaced with
sizeof(*array) / sizeof((*array)[0])
Or something similar.
{ std::cout << "array[" << i << "," << j << "] =" << array[j][i]
Hmmm... what have we here? i goes from 0 to 4... but your second index only goes from 0 to 2. You are going outside your array, causing undefined behavior. This could have been caught if you had used std::vector with the at() function.
<< std::endl; } } }
int main(int argc , char * argv[]) { int a[5][3] = { {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0} };
a[0][0] = 1; a[2][4] = 4;
Again you've swapped indexes and gone outside the bounds of your array.
f(a);
return 0; }
The final and most obvious problem with your code is that your function is completely incapable of dealing with 2-d arrays that have a different number of columns.
std::vector is usually a better solution, as it appears it would have been in this case.
I understand there are issues with my example. I know there were
magic numbers, and other issues. I wasn't providing my code as bullet
proof. My point was that people automatically start saying "use
std::vector" when they don't understand the scope of the problem. The
original post asked about doing multidimensional arrays NOT
std::vector! Everybody only says use the STL. SOMETIMES YOU CAN'T USE
THE STL. That was my point!
Big Brian wrote: Everybody seems to forget that C++ does support multidimensional arrays in the language.
Sure. But the OP specifically requested a multidimensional array
with *dynamic sizing* capabilities.
Everybody imediately suggests using the STL containers,
which is perfect for the OP's request.
but sometimes this isn't acceptable, and you need to use the built in arrays.
True. But making this dynamic isn't that simple any more
compared to std::vector
--
Karl Heinz Buchegger kb******@gascad.at This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tino Lange |
last post by:
Hi!
I identified a bottleneck in my programs.
I just want to "encrypt" data by easy xoring. Ok - that's no
encryption at all - I know. But it's hardly readable - and that's
enough :-) Just...
|
by: andy.dreistadt |
last post by:
Hi all,
I came across another problem that is probably pretty easy but, again,
due to my rusty-ness with C, I'm a little stumped.
I have a struct that looks like this:
/* Instrument Data...
|
by: Mike P |
last post by:
I have, what should be, a simple scope problem. Can you help me fix this?
I'm trying to end up like this:
originalArray = and newArray = .
Instead I wind up like this:
originalArray = and...
|
by: John Salerno |
last post by:
Is this valid:
string allSwitchValues = readSwitches.ReadToEnd().Split('|');
I have no way to test it just yet (although it didn't seem to create
compile errors). Originally I had it divided...
|
by: Brian Henry |
last post by:
I have a listbox, and i need to show a text string for selection, but each
item is tied to a number (not visible) i need to select the visible text and
have it return the number value, i know its...
|
by: rodchar |
last post by:
Hey all,
How do I add an element to an already populated array?
dim myary() as string
thanks,
rodchar
|
by: jimi_xyz |
last post by:
I am kind of new to C sharp; I have a quick question I have multiple
text boxes 12 of them to be exact. What I want to do is fill them with
values that are in a array of integers. The way I am...
|
by: mab464 |
last post by:
I have this code on my WAMP server running on my XP machine
if ( isset( $_POST ) )
{
for($i=0; $i<count($_POST);$i++)
{
if ($ans != NULL )
$ans .= ", " . $_POST ; // Not the first...
|
by: Tony Johansson |
last post by:
Hello!
As I have understood this it means the following. Correct me if I'm wrong.
You store type objects in the array obj and the first one is refering to
a and the second is refering to b and...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |