|
|
View previous topic :: View next topic |
Author |
Message |
rmozer
Joined: 06 Jun 2005 Posts: 17
|
Passing constant string to a function? |
Posted: Mon Jun 06, 2005 9:11 pm |
|
|
I understand that you can not pass a pointer to a constant in PICC. Are there any clever workarounds?
As an example, in HiTech C you can do something like:
Code: |
void MyFunction(const char *string1, const char *string2)
{
// code to do something with the two strings...
}
void main()
{
MyFunction("string 1","string 2");
} |
Using PICC, you cannot use the const construct as a parameter. With out that you get the error "Attempt to create a pointer to a constant".
Is there an easy or nifty way to pass multiple const strings to a function for processing?
What I am trying to do is port MicroChips AN724 to PICC for establishing a PPP connection. From there I will try to implement some of the ideas from "TCP/IP Lean" on a 18F6720. Does anybody already have something like this that they would like to share? |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 07, 2005 2:54 am |
|
|
Yes, there is a sort of workround, but it depends what you want to do.
If you declare a function to receive an integer, so (for example):
Code: |
void myfunc(int8 val) {
//Do something with the integer here
}
|
Then you can call this function with:
myfunction("Constant string");
and the compiler will automatically call the function for each character in the constant string in turn.
The 'downside' here is that it only really works for a single string passed to a function. The HiTech approach is not a 'workround', it actually does handle pointers to constant strings, but because they are in a seperate memory space, this extra data has to be provided as part of the definition. The downside though is that the result is quite bulky.
It is worth remembering that on the latter chips (18F family), that support I/O to the program memory, you can declare a string and use it like this:
Code: |
int8 read_byte(int16 address) {
//This is a function to allow the program memory to be accessed
//in bytes (I hope!..).
static int16 oaddress;
static int16 value;
if (oaddress!=(address>>1)) {
oaddress=address>>1;
value = read_program_eeprom(oaddress);
}
return((address&1)?value>>8:value & 0xFF);
}
int8 myfunction(int16 address1, int16 address2) {
//Here you use the two addresses in a manner like 'pointers',
//with the 'read_byte' function.
int8 first_byte,second_byte;
address1*=2;
address2*=2;
first_byte=read_byte(address1)
second_byte=read_byte(address2);
return (first_byte & second_byte);
//Return the bitwise 'and' of the characters at the start of each string.
}
//Then call this with:
const first_string = "This is the first";
const second_string = "The second";
val=myfunction(label_address(first_string),label_address(second_string));
|
Now this is 'off the cuff' untested code, which may well have some errors, but I have used a similar approach in the past which worked OK.
The ''label_address' function, returns the location in memory where the string is stored. This can then be used to access the string using the read_program_eeprom function. The only problem is that these all work in 'words', while for string accesses 'bytes' are easier. The 'read_byte' function, attempts to hide this from the user, by taking the word address*2, and accessing the memory on a 'byte' basis. I must admit when the label_address function appeared, I was a little suprised that it was not followed by a crude 'constant string access' library, for the chips supporting this type of I/O.
Best Wishes. |
|
|
rmozer
Joined: 06 Jun 2005 Posts: 17
|
|
Posted: Tue Jun 07, 2005 1:41 pm |
|
|
Thanks... That let me to several solutions ;) |
|
|
pablodecker
Joined: 28 Aug 2007 Posts: 5
|
Thanks |
Posted: Thu Mar 26, 2009 6:49 am |
|
|
Thanks Ttelmah for your help!!! |
|
|
|
|
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
|