View previous topic :: View next topic |
Author |
Message |
jahan
Joined: 04 Apr 2005 Posts: 63
|
passing dat[4] to a subroutine |
Posted: Tue Jul 12, 2005 9:37 am |
|
|
I have the following subroutine:
Code: |
int Validate(int a, int b, int c, int d) {
if (a == b) && (c == d)
return 1;
else
return 0;
}
void main(void) {
char dat[4];
dat[0] = '1';
dat[1] = '1';
dat[2] = '2';
dat[3] = '3';
if (Validate(dat[0], dat[1], dat[2], dat[3]))
output_high (PIN_A0);
}
|
How do I pass the pointer to the dat[4] to the function and use it inside the function rather than sending one char at a time as parameter to the function?
Is there a better way of doing this?
Thankx |
|
|
valemike Guest
|
|
Posted: Tue Jul 12, 2005 10:15 am |
|
|
Look how i pass in the pointer to the array. You simply call the array by name: Validate(dat)
And you can index into an array with a pointer to a char -- pChar.
pChar[2] is the same thing as writing *(pChar + 2)
Code: |
#include <18F448.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#ignore_warnings 202
#case
int Validate(char *pChar);
int Validate(int *pChar)
{
int x;
for (x=0; x<4; x++)
{
printf ("%d: %c\r\n", x, pChar[x]);
}
return 0;
}
void main(void)
{
char dat[4];
dat[0] = '1';
dat[1] = '1';
dat[2] = '2';
dat[3] = '3';
Validate(dat);
while (1);
}
|
The output of this program is:
0: 1
1: 2
2: 2
3: 3 |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jul 12, 2005 10:19 am |
|
|
Try Code: | int Validate(const char* dat) {
if (dat[0] == dat[1]) && (dat[2] == dat[3])
return 1;
else
return 0;
}
void main(void) {
char dat[4];
dat[0] = '1';
dat[1] = '1';
dat[2] = '2';
dat[3] = '3';
if (Validate(dat))
output_high (PIN_A0);
} |
I recommend to check the resulting size of this function as compared to your original version. The CCS compiler is very efficient when it knows the exact address of every variable. As soon as you start using pointers it is impossible for the compiler to calculate the addresses of the variables and it will generate more universal but larger code. You might be surprised to find out that your original version is larger in the editor than the second version, but smaller in the compiled version.
Edit: It's no fun to find out some other person answered the question while I was typing this message.
Last edited by ckielstra on Tue Jul 12, 2005 10:21 am; edited 1 time in total |
|
|
Guest
|
|
Posted: Tue Jul 12, 2005 10:19 am |
|
|
valemike wrote: | Look how i pass in the pointer to the array. You simply call the array by name: |
Sorry, I said it wrong. What I meant to say is that I pass in the ADDRESS of the array, or in other words, the address of the first element of the array.
Since pChar is a pointer to a char, then pChar now points to the first element in the array upon calling the Validate function. |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Tue Jul 12, 2005 10:37 am |
|
|
Quote: | It's no fun to find out some other person answered the question while I was typing this message. |
I showed a demo program, and you showed exactly how the program was to be corrected. I'm sure both will help.
I can picture us all right now, when we're bored, we sit and keep refreshing the CCS forum on the browser wondering if a new post has popped up the last 3 minutes. (me, ckielstra, Ttelmah, and especially PCMPro & Mark) |
|
|
jahan
Joined: 04 Apr 2005 Posts: 63
|
|
Posted: Tue Jul 12, 2005 11:55 am |
|
|
Is there any difference between:
and
and having the world "CONST" for the parameter. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Jul 12, 2005 12:41 pm |
|
|
jahan wrote: | Is there any difference between:
and
|
no
Quote: |
and having the world "CONST" for the parameter. |
not allowed |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
valemike |
Posted: Tue Jul 12, 2005 12:46 pm |
|
|
const char* dat
Isn't the above allowed as far as ANSII C goes? I think it declares dat to be a pointer to a char that is read-only, and thus the pointee's contents can't be changed.
Whether or not the CCS compiler would interpret it this way is a different story. |
|
|
jahan
Joined: 04 Apr 2005 Posts: 63
|
|
Posted: Tue Jul 12, 2005 1:18 pm |
|
|
ckielstra wrote: | Code: | int Validate(const char* dat) {
if (dat[0] == dat[1]) && (dat[2] == dat[3])
return 1;
else
return 0;
}
void main(void) {
char dat[4];
dat[0] = '1';
dat[1] = '1';
dat[2] = '2';
dat[3] = '3';
if (Validate(dat))
output_high (PIN_A0);
} |
|
is above example mistyped with word CONST in parameter part? |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
valemike |
Posted: Tue Jul 12, 2005 1:47 pm |
|
|
With the const, it compiles in Unix. However, it does not compile in CCS.
So delete the const, and you'll be okay. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jul 12, 2005 3:19 pm |
|
|
Sorry about the const keyword, it is indeed not valid for the CCS compiler to use it the way I did. I've been working too much in Delphi lately. C++ allows this construct as well.
The const keyword is valid in CCS, but only to declare a constant like for example:
Code: | int8 const A = 3;
char const *B = 4;
int8 const C[3] = {1,2,3}; |
And yes, I checked these in v3.225.
In C++ there are even more options:
Code: | const int* pX; // A changeable pointer to a constant int.
int* const pY; // constant pointer to changeable int
const int* const pZ; // const pointer to const int |
See also http://www.possibility.com/Cpp/const.html for a more in depth description.
Good thing about these constructions is that it allows the compiler to do some extra type checking and create better optimized code. |
|
|
|