Passing array into a function

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
Hi guys,

I have this array :
unsigned char pat[8] = {0xC3,0xC3,0xC3,0xFF,0xFF,0xC3,0xC3,0xC3,}

How can I pass that array into this function ?

void display(unsigned char pat[])

{
unsigned int cnt, col, row, num ;

row = 1;
//for (cnt = num*8 ; cnt < (num*8+8) ; cnt ++ ) //display pattern each character
for (cnt = 0 ; cnt < 8 ; cnt ++ ) //display pattern each character
{
P3 = ~pat[cnt];
P1 = ~row;

delay_ms(10) ; // delay of 1 ms
row = row<<1;


}


row = 0x00;

}


Thanks
 

I think you need to read something like **broken link removed**


You are currently using the same name in the array name and function parameter and this can be confising so suppose it is like this

Code:
unsigned char pat[8] = {0xC3,0xC3,0xC3,0xFF,0xFF,0xC3,0xC3,0xC3,}

void display(unsigned char fun_pat[]) {

}

All you can send to a function is a pointer to an array.
Note the importance of this because is you change a value of what you think as a copy of the original array (for example fun_pat[cnt]=5) you will actually change the data of the par array pat[cnt]=5;

When inside the function you access the data of the pointer using fun_pat[cnt] it is the same as using *(fan_pat+cnt) which of course points to the data of your array pat[cnt] which is declared out of the function,

A read at **broken link removed** will help on this.

Alex
 
just change your function definition as the following:

void display(unsigned char* pat)

{
unsigned int cnt, col, row, num ;

row = 1;
//for (cnt = num*8 ; cnt < (num*8+8) ; cnt ++ ) //display pattern each character
for (cnt = 0 ; cnt < 8 ; cnt ++ ) //display pattern each character
{
P3 = ~(*(pat+cnt) );
P1 = ~row;

delay_ms(10) ; // delay of 1 ms
row = row<<1;


}


row = 0x00;

}
 

He doesn't need to change his code, it is perfectly operational as it is , you are just doing the same thing using different coding.
*(pat+cnt)
and
pat[cnt] are exactly the same

and unsigned char pat is already declared as a pointer from the compiler so using unsigned char* pat doesn't make a difference

Alex
 



Thanks for the info

When I did :
unsigned char pat2[8] = {0xFF,0x03,0x03,0x3F,0x3F,0x03,0x03,0xFF,};//E

display(pat);
delay_ms(15000);
display(pat2);
delay_ms(15000);


Why didn't the pattern scrolled properly ? I saw I shadow between those patterns, do you guys have any ideas ?
 

I think that what you are asking is not related to the original question or the title of this thread and has to do with the operation of your function , not the actual parameter passing.
I would suggest you create a new thread where you can describe what you want to do, share your circuit and the errors you get.

Alex
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…