Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

As we've seen throughout this chapter, it's straightforward to manipulate the elements of an array using pointers, so there's no particular insurmountable difficulty if getline receives a pointer. One question remains, though: we had been defining getline with its line parameter declared as an array:

Code:
[COLOR="#FF0000"]int getline(char line[], int max)
	{
	...
	}[/COLOR]

We mentioned that we didn't have to specify a size for the line parameter, with the explanation that getline really used the array in its caller, where the actual size was specified. But that declaration certainly does look like an array--how can it work when getline actually receives a pointer?

The answer is that the C compiler does a little something behind your back. It knows that whenever you mention an array name in an expression, it (the compiler) generates a pointer to the array's first element. Therefore, it knows that a function can never actually receive an array as a parameter. Therefore, whenever it sees you defining a function that seems to accept an array as a parameter, the compiler quietly pretends that you had declared it as accepting a pointer, instead. The definition of getline above is compiled exactly as if it had been written

Code:
[COLOR="#FF0000"]int getline(char *line, int max)
	{
	...
	}[/COLOR]

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
 

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

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top