CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Two dimension array as Function Parameter

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ysaacb



Joined: 12 Jun 2006
Posts: 19

View user's profile Send private message

Two dimension array as Function Parameter
PostPosted: Thu Oct 25, 2007 5:41 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 25, 2007 6:28 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 28, 2007 10:42 am     Reply with quote

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







PostPosted: Sun Oct 28, 2007 11:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 30, 2007 3:25 am     Reply with quote

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








PostPosted: Thu Jan 31, 2008 5:24 am     Reply with quote

Dimensions restricted by 5x5
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group