|
|
View previous topic :: View next topic |
Author |
Message |
John_Lintern
Joined: 30 Sep 2004 Posts: 14
|
Problems using different size arrays with a function HELP ! |
Posted: Fri May 13, 2005 1:51 am |
|
|
I am having problems using arrays and a function.
I am trying to pass two variables in the function,
- a pointer to the start address of the array
- a variable to define the length of the array
There's a global variable which the function will use to compare it with the data in the array.
The function will increment a pointer from the start address of the array and will stop if it reaches the end of the array.
Because the arrays are of different sizes it needs to know the length of the array.
The function will then calculate an 8-bit value to return (the actual code for the function is not yet complete).
All my arrays are of the size 2 by y, where y could be any 8-bit value.
For example,
BYTE CONST ArrayOne[2][4] = {{1,2,3,4}, {5,6,7,8}};
BYTE CONST ArrayTwo[2][6] = {{9,10,11,12,13,14}, {15,16,17,18,19,20}};
BYTE CONST ArrayThree[2][3] = {{21,22,23}, {24,25,26}};
BYTE CONST ArrayFour[2][5] = {{27,28,29,30,31}, {32,33,34,35,36}};
So I only need to send the y length of the array, the x length will always be 2.
The function (called ProcArray) is defined at the top of my program like this....
Code: |
int ProcArray(*int, int);
|
The call to the function is defined like this....
Code: |
Pressure=ProcArray(ArrayOne[0],4);
|
And the function itself looks like this....
Code: |
int ProcArray(int *ArrayPointer, int Max_y)
{
.
.
.
...... code to compare global variable with the data in ......
...... the array and calculate an 8-bit value to return ......
.
.
.
}
|
But I cannot compile the program because I keep getting the error "Bad Expression Syntax" on the line where I call the function...
Code: |
Pressure=ProcArray(ArrayOne[0],4);
|
How can I get this to work ?
I am not even sure if I have used the right format to define my function at the top of the program ?
I thought the format should be...
Code: |
int ProcArray(int*, int);
|
not...
Code: |
int ProcArray(*int, int);
|
Please help !
I know what I want to do, I just dont know how to do it ! |
|
|
Ttelmah Guest
|
|
Posted: Fri May 13, 2005 2:27 am |
|
|
Unfortunately, the main problem here is basic, and fundamental. You cannot have pointers to constant arrays.
Seperately, there is a syntax problem. The declaration:
Code: |
Pressure=ProcArray(ArrayOne[0],4);
|
Passes the contents of ArrayOne[0] to the function. You need to use:
Code: |
Pressure=ProcArray(&ArrayOne[0],4);
|
Which passes the address to the function.
So change the array declarations to:
BYTE ArrayOne[2][4] = {{1,2,3,4}, {5,6,7,8}};
BYTE ArrayTwo[2][6] = {{9,10,11,12,13,14}, {15,16,17,18,19,20}};
BYTE ArrayThree[2][3] = {{21,22,23}, {24,25,26}};
BYTE ArrayFour[2][5] = {{27,28,29,30,31}, {32,33,34,35,36}};
Change the calls to pass the addresses, and change the declarations to:
int ProcArray(int *ArrayPointer, int Max_y)
Put the variable name in the pre-declaration as well.
Best Wishes |
|
|
JPA Guest
|
|
Posted: Fri May 13, 2005 2:32 am |
|
|
1) I think that pointers to constant array are not allowed in CCS.
2) int ProcArray(*int, int); is not a valid C syntax; use int ProcArray(int *, int);
I think what you are trying is valid C, but I have some doubts about CCS C behaviour with these (I had strange behaviour when using pointers to structure... "*ptr_to_struct = struct" did not worked in some cases, I had to use memcpy...) |
|
|
JPA Guest
|
|
Posted: Fri May 13, 2005 2:40 am |
|
|
Ttelmah wrote: |
Seperately, there is a syntax problem. The declaration:
Code: |
Pressure=ProcArray(ArrayOne[0],4);
|
Passes the contents of ArrayOne[0] to the function. You need to use:
Code: |
Pressure=ProcArray(&ArrayOne[0],4);
|
|
If my memory is good (I try to avoid using complex pointer in my PIC programs, but used them quite a lot when developping on Unix workstation long time ago), if you declare "int Tab[2][4]",
- Tab[i][j] is an int
- Tab[i] is a pointer to int (int *)
- Tab is a pointer to a pointer to int (int **)
If I am right, the call
Code: |
Pressure=ProcArray(ArrayOne[0],4);
|
is correct, but I a not sure of it. |
|
|
|
|
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
|