Jump to content

Recommended Posts

i try to include a 2 dimensional arrays in function calls to have functions doing some modifications to arrays.

a very simple example to initialize the array is:

 

unsigned char MATRIX[15][16];

 

void Init (unsigned char *myMATRIX) {

unsigned char x,y;

for (x=0;x<15;x++) {

for (y=0;y<16;y++) {

myMATRIX[x][y] = x+y;

}

}

}

 

void main (void) {

Init(MATRIX);

}

 

i would expect that a memory area of 240 bytes would be reserved for the array variable MATRIX, and when calling the Init function, the start address of this memory range would be given to the Init function. then, the whole MATRIX would be initialized.

 

fact is (same behaviour in simulator and on targed) that bytes from address 0x100 are written up to address 0x10f. then, instead of proceeding with address 0x110, it again writes to addresses 0x100 to 0x10f. addresses above 0x10f are never written.

 

what am i doing wrong?

i think my syntax isn't too bad ;-(

 

thanx for your help!

 

Share this post


Link to post
Share on other sites

Hi

 

 

Memory maping is PIC dependent and that seems a possible memory banking problem.

What PIC are you using?

 

BTW

0x100 + 240 = 0x1f0

 

 

Best regards

Jorge

Share this post


Link to post
Share on other sites

I think you are not being nice to the compiler as it has no idea the dimensions of your array. It thinks you just passed a 1D array when you in fact passed a 2D array.

 

Maybe try:

 

void Init (unsigned char myMATRIX[][16]) {

 

to let it know that it is a 2D array where the smallest index is 16. This way the compiler can calculate the offsets properly.

 

If this does not work, maybe skip passing a pointer to MATRIX and just use the global variable inside your routine.

Share this post


Link to post
Share on other sites

i really tried to be nice ;-)

 

i already tried different ways to tell the compiler about array size...and all i get is an "error: missing right paren". that's all ;-(

 

using global access is no way to go, as i have multiples of these arrays, but all of them have to be processed using the same functions.

 

the only way i see so far is to convert to a 1D array and calculate position myself, although i think that ansi c should support 2D arrays in functions.

 

thanx!

Share this post


Link to post
Share on other sites

Hi

 

In "C" arrays are not a native type, so have allways been a source of confusion, in particular to someone used to programming languages where they are a native type.

 

An array in "C" is a piece of contiguos memory adressed by a pointer.

The declaration "unsigned char MATRIX[15][16];" should be read as "allocate a contiguos piece of RAM with size 15 x 16 x sizeof(unsigned char) and a pointer to it named MATRIX.

The efective result is an unsigned char pointer to the first byte of a chunk of 240 bytes.

 

The "[]" construct behaves like an allocation operator, and "MATRIX" is in fact a pointer to unsigned char.

 

The most common practice on initializing an array is something like this

#include <system.h>

#define N_LINES     15
#define N_COLUMNS    16

unsigned char Matrix[N_LINES][N_COLUMNS];

void Init (unsigned char *myMatrix)
{
  unsigned char *p = myMatrix;

      for (unsigned char x=0;x<N_LINES;x++)
        {
        for (unsigned char y=0;y<N_COLUMNS;y++)
            {
             *p++ = x+y;
            }
        }
} // void Init (unsigned char *myMatrix)

 
void main (void)
{
        Init(Matrix);
        while(1);
} // void main (void)

We must not forget that arrays and strings are not a native 'C' types.

 

The behaviour of multiple brackets in the parameter list of a function is mostly implementation dependent.

As for the single empty bracket "[]" used as sufix its just the same as an "*" used as a prefix it declares or dereferences a pointer.

 

 

Juts my 2 cents...

 

 

Best regards

Jorge

Edited by JorgeF

Share this post


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...