Jump to content

Using Multidimensional Arrays In Functions


Recommended Posts

i have a multidimensional array, or let's say an array of string, that i would like to use in a function.

i tried ma make an exemple:

 

unsigned char myarray[15][6];

 

these are 6 strings of the lenght of 15 characters. right?

 

now i want to give this to a function by reference:

 

PrintMyArray(myarray);

 

 

 

the function itself is defined as:

 

void PrintMyArray(unsigned char *thearray) {

 for (unsigned char i=0; i<6;i++) {
printF(thearray[i];
 }
}

 

 

unfortunately, this does not work.

what do i have to do to give an array to function?

 

thanx!

Link to post
Share on other sites

to make it a bit more clear, here the test proggy:

 

#include <system.h>
#include "SndSer18a1.h"


void main (void) {

 unsigned char myarray[15][6];

 PrintMyArray(myarray);
}



void PrintMyArray(unsigned char *thearray) {

 for (unsigned char i=0; i<6;i++) {
SsTxt(thearray[i]);
 }
}



void SsTxt(const unsigned char *text) {

 unsigned char loop;		

 loop = 0;
 while ((text[loop] !=0) && (loop < 255)) {
SsSend(text[loop++]);				   
 }
}

 

 

i get an error message for the line

 

SsTxt(thearray);

 

which tries to give a single line of text to the SsTxt function.

when i modify to

 

SsTxt(thearray);

 

i get no more error, but this will for sure access the whole array instead of only a line.

 

how do i have to modify to access single lines of text in my array?

 

thanx!

Link to post
Share on other sites

Hi Bongo,

 

I don't think SourceBoost C works very well with multi-dimentional arrays, or it just doesn't work for me.

 

This code gives me an error about missig right parenthesis:

 

#include "system.h"

char myArray[6][15];

void myFunc(char arry[][15]);

void main(void)
{
myFunc(myArray);
}

void myFunc(char arry[][15])
{

}

 

Note in your program that it is the number of elements that comes first then the size of the element.

 

i.e. char myArray[6][15] is 6 strings of 15 chars.

 

Cheers

 

Reynard

Link to post
Share on other sites

Something like this will do:

 

#include "system.h"

char myArray[15][6];

void myFunc(char*);

void main(void)
{
myFunc(myArray);
}

void myFunc(char *arry)
{
unsigned char i;
for( i = 0; i < 15; i++ )
	SsTxt(&arry[i*6]);
}

 

or

 

#include "system.h"

char myArray[15][6];

void myFunc(char*);

void main(void)
{
unsigned char i;
for( i = 0; i < 15; i++ )
	myFunc(myArray[i]);
}

void myFunc(char *text)
{
SsTxt(text);
}

 

Regards,

Pavel

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...