# Thread: PASSINAG A bidimansional array at a function

1. ## PASSINAG A bidimansional array at a function

how do I pass as referance a bidimansional array ata function?

this
int a[1][2];
void f(int **b);
doesnt work ! type mismatch.

-i found this way ...
struct c
{
int *p[2];
} array;

voit initNewType()
{
array.p[0] = &(a[0][0]);
array.p[1] = &(a[0][1]);
}

void f(c *value)
{ //how do i move the array index thru the bidimansioal array?????
*((*value).p[i]+j); //value at a[j][i]....but there 's a mistake someone can help me?
//there's a simpler solution instead that using this mess?

}
main()
{ f(&array);
}

///there's a esyer way to pass by referance a duble array?

if you emailme to bruno.marovelli@fastwebnet.it i'll really apprciate it!

2. Code:
```int a[1][2];
void f(int b[1][2]);```
Use that until you're more comfortable with arrays.

3. i'M CONFORTABLE WITH ARRAYS BUT...passing a duble array by referance at a function isn't a easy task that's all ,infact...all people respond me with performing a local copy...but this makes computing really slower and here is a fastnest matter topic.

4. Arrays are never passed by value or copy.

On an aside, why do you have a 1 for the size of one of your array dimensions? There's no point. Just use a single dimension array, because having 1 does nothing useful.

Code:
```char array[1][2]; /*this, is in effect... */
char array[2]; /* the same as this... */```
[/edit]

Quzah.

5. No
becouse the method must work for general dimension bidimensional arrays for example:
int a[100][203]
this array rappresent an image data for example and referance rewuired to make things faster.
And then...coping the array the way,i didn't think today about it, but it's interesting ,this way:
int a[12][2];
f(int b[12][2]);
main()
{ f(a);

}

however my first solution is faster...the one in the initial thread lquestion i posted...i need something that permets me to pass a bidimensional array by referance.
Someone have the TC++ Borland Paper Guide? I haven't got it anymore...but there...must be an explanation how to make it!

6. Arrays are always passed by reference - no matter how you dress them up

Code:
```int a[12][2];

// All these prototypes are equivalent
f(int b[12][2]);
f(int b[][2]);
f(int (*b)[2]);

int main()
{
f(a);
return 0;
}```
I wonder if you're trying to pass
int a[10][10];
int b[20][40];
to the same function.
If so, read this http://www.eskimo.com/~scs/C-faq/q6.19.html

And use code tags!

7. ## are you kidding?

well not at all...do you think I'm stupid?
Can you understand what I wrote?
Sure It's not possible to pass different bidimesiona arrays with to a function defined f(ina a[2][3]) becouse c++ perform a array member to member copy offcourse!
But your suppositions letme think you don't understend pretty much what you talk about or reading.....:-|

8. ## no need something more fine working

However your solution again doesent perform a by referance passing to the function but performs a copy of the passed array to the local array to the function and if the array passed ad declared in the function proptipe is quite huge time CPU computing an memory space is widely wasted!
That's not high performed coding...and sound really stupid!

9. and array aren't passed by referance.At least that you specifically specify that.
Infact you shouldent define a prototipe so:
f(int b[4][4]);
int a[4][4];
to pass an array.. infact
int a[4];
f(int *b);
main()
{
f(a); //this is a by referance passing of the a array
}

10. No it doesn't. You're wrong.
Code:
```void foo( char bar[] )
{
bar[0] = 'N';
bar[1] = 'o';
bar[2] = ';';
}

int main( void )
{
char array[] = "Hi! You're still wrong."

printf("%s\n", array );
foo( array );
printf("%s\n", array );

return 0;
}```
Run that.

So you're telling me that this is going to:
1) Allocate the first array.
2) Call the function.
3) Make a duplicate of the first array inside the function.
4) Copy everything from the first array into the new local array.
5) Modify the local array.
6) Copy everything from the new local array back into the first array.

Is that what you're trying to say? Well, you're wrong. I know what it's like to want to be right. But trust us on this one, you're wrong. I suppose someone could quote the standard, but I doubt you'd believe that either.

Quzah.

11. no !
In your wxample there's no local copy but a reference char array passing ...so the two printfs will output different results!
But
int a[2][3];
void f(int b[2][3]);

main()
{
f(a); //will not modify a if computational is performed inside f on b

}

12. undefinedundefinedundefined
there 's a local copy

13. >f(a); //will not modify a if computational is performed inside f on b

Have you tried it?

14. Well
ok,it's right...but how do I solve this?
a[may chage][may chage]

f(b[must thake in count that change][must take in count that change]);
????

15. Code:
```#include <stdio.h>
char a[2][4] = {"Not", "So"};

void f(char b[2][4]) {
b[1][2] = '!';
b[0][1] = '(';
}

int main(void)
{
puts(a[0]);
puts(a[1]);
f(a); //will not modify a if computational is performed inside f on b
puts(a[0]);
puts(a[1]);
return 0;
}```
Run that and learn why you are wrong.

Popular pages Recent additions