This is a really basic question, but i can't find the answer ANYWHERE. Basically, how do you pass a multidimensional array as an argument?
Code://this works: won(ar[]) { } //this does not work won(ar[][]) { }
This is a really basic question, but i can't find the answer ANYWHERE. Basically, how do you pass a multidimensional array as an argument?
Code://this works: won(ar[]) { } //this does not work won(ar[][]) { }
thx
Give everything types: return type, parameter type.
The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.
If you have
int array[2][3][4];
You declare and define the function with the following parameter
void myfunc ( int array[2][3][4] );
Quite simply, you just copy/paste the declaration of the array you want to pass to the function.
And you call this function like so
myfunc( array );
> //this does not work
> won(ar[][])
You are only allowed to leave the left-most dimension empty. All the others must be present.
This is equivalent to the above example
void myfunc ( int array[][3][4] );
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Correct me if I'm wrong but wouldn't passing a pointer to the array be better than passing the entire array?
Or does the compiler then get a pointer to the start of the array on the stack and then simply use offsets from there.
Now that I've thought about it, the only disadvantage of passing it via the stack is the cleanup that needs to be done on exiting the function.
Perhaps execution within the function would not suffer at all.
it makes a copy of the array on the stack. yes pointers would be better.
Try to help all less knowledgeable than yourself, within
the limits provided by time, complexity and tolerance.
- Nor
Hmm~
I did not know you could pass arrarys through functions like that.. I thought only pointers to array elements would be more efficient and a lot more safer than passing an entire array.. Well I didn't even know the idea of passing an array existed.. Wouldn't that generate some sort of error?..
Farewell~
DCII764II00
when you call a functions like
DoWhatIWant(int this_long);
what happens is the program makes a copy of this_long and places it on the stack.Code:DoWhatIWant(172);
one copy is what you used to call your function and the other is what the function is using. if you make any changes to this_long with in the function when it exits/return the copy your functions was using poped off the stack.
t things arize. one is Scope
the other is a problem with memory. if you have 512 megs of ram and you pass an array[300megs big] and not a pointer to it then the compiler will make a copy and the function will use it.
now some math
(300+300=600) (512-600 = [b]memory error[/red])
and the process of copying the varaible to the stack takes some time.
now if you pass a pointer to your array your compiler will use the memory at the address the pointer points to... something like that anyways it saves the cpu the trouple of having to copy a VERY large amount of data.
DoWhatIWant(int* this_long);
Try to help all less knowledgeable than yourself, within
the limits provided by time, complexity and tolerance.
- Nor
You can use pointers but i think if you want to do it that way then you need to pass it with range of the 2nd subscript so given the following:Hope that helps.Code:int array[5][7]; //define the array thus: "return type" arrayfunc([5][7]); //1st subscript optional me thinks //then call your function like so: arrayfunc(array);
And you are guaranteed with a pointer that has already been correctly initialized that if you return a reference to what it points to...you will never be pointing to invalid memory.now if you pass a pointer to your array your compiler will use the memory at the address the pointer points to... something like that anyways it saves the cpu the trouple of having to copy a VERY large amount of data.
Making a copy of the array if and when you change it will most certainly eat cycles although I can think of several ways in assembly that this could be worked around - but it will still take more cycles than just passing a pointer to an array.
When you pass an array as a function parameter there is a conversion to a pointer to the first element of the array. So, when you pass an array you don't pass the array elements by value, you pass a pointer to the first element of the array.
well it's true when youre passing an array to a function you're really passing a pointer to the array, it doesn't make a copy of the array
http://www.cplusplus.com/doc/tutorial/tut3-1.html
> it makes a copy of the array on the stack. yes pointers would be better.
No it doesn't!
All arrays are passed as "pointers to first element" of the array, no matter how you declare the function prototype.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.