View previous topic :: View next topic |
Author |
Message |
ysaacb
Joined: 12 Jun 2006 Posts: 19
|
Two dimension array as Function Parameter |
Posted: Thu Oct 25, 2007 5:41 am |
|
|
Hi Everibody
How can I a pass a two dimension array asa function parameter?
Regards
Ysaac |
|
|
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Thu Oct 25, 2007 6:28 am |
|
|
Pass the name of the array (it's a pointer to the array). Note, however, that the function does not know the dimensions.
e.g.
[code]
void myfunc (char *p)
{
..
}
{
char a[10][5];
myfunction (a);
}
[/code]
should work ok.
Ken |
|
|
ysaacb
Joined: 12 Jun 2006 Posts: 19
|
|
Posted: Sun Oct 28, 2007 10:42 am |
|
|
I'm trying to do something like this i.e. make the array point to the adress of a
void myfunc (char *p)
{
char BUFFER[7][32];
buffer=a;
..
}
{
char a[10][5];
Regards
Ysaac
myfunction (a);
} |
|
|
Ttelmah Guest
|
|
Posted: Sun Oct 28, 2007 11:14 am |
|
|
You are not goint to get very far with this.
The declaration:
char BUFFER[7][32];
Actually _creates_the array. So you are immediately using 224 bytes of storage. An array name is a _constant_, so you can't then point this to another external array. You need to declare 'buffer' as:
char *buffer;
Which is then a variable that can be used to point to an array.
You then seem to want to use different dimensions inside the function to outside. A sure way of destroying memory data....
What you can do, is pass a pointer to a function, for use as an array, but you have to specify _one_ dimension.
Code: |
void myfunc (char buffer[][5])
{
}
{
char a[10][5];
myfunction (a);
}
|
Then allows 'buffer' to access the array data stored in 'a'.
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Tue Oct 30, 2007 3:25 am |
|
|
A couple of things. In your code in the function you are doing :-
buffer = a;
Your value is actually p as defined in the function header so
buffer=p;
char BUFFER[7][32]; is not required unless you are using BUFFER for something else as C is case sensitive.
So you are almost there :-
void myfunc(char *p)
{
char *buffer = p;
buffer[0][0] = 'a';
buffer[0][1] = 'b';
strcpy(buffer[1], "Hello");
}
void main()
{
char a[10][5];
myfunction(1);
}
Should work and do what you want.
Actually unless you need to keep a reference to the start of the array (p) or you are not actually going to modify the pointer (p) you can do away with buffer and just do
p[0][0] = 'a';
etc... |
|
|
Guest
|
|
Posted: Thu Jan 31, 2008 5:24 am |
|
|
Dimensions restricted by 5x5 |
|
|
|