|
|
View previous topic :: View next topic |
Author |
Message |
fmartinezs
Joined: 16 Aug 2006 Posts: 19
|
Help with string parameters in functions |
Posted: Fri Aug 18, 2006 2:19 pm |
|
|
Hi:
·I'm a little confused about string handling. I need to implement the next function: Given 2 16-bytes strings (a,b), generate a 3rd string(c), where
each element of c is the result of XORing the respective elements of a and b. Something like this:
Code: |
int i;
char a[16],b[16],c[16];
strcpy(a,"ABCDEFGHIJKLMNOP");
strcpy(b,"abcdefghijklmnop");
for(i=0;i<=15;i++)
c[i] = a[i] ^ b[i];
|
·This way it works OK. But I need to implement a function where a and b are entered as parameters. I've tried unsuccessfully to define this function (won't compile), so this is the code I wrote:
Code: |
char XorBytes(char b1[16], char b2[16])
{
int aux,i;
char temp[16];
for(i=0;i<=15;i++)
temp[i] = b1[i] ^ b2[i];
for(i=0;i<=15;i++)
aux = temp[i];
return temp;
}
void main()
{
int i,aux;
char a[16],b[16],c;
strcpy(a,"ABCDEFGHIJKLMNOP");
strcpy(b,"abcdefghijklmnop");
c = XorBytes(a,b);
sleep();
} |
·As you see, it's not very functional; the aux variable is used to debug the value of each element of the string, using a breakpoint and the Watch window within MPLAB.
·It seems i'm not passing the arguments to the function the right way... If you can help me, i'll be grateful.
Best regards,
Fernando |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 18, 2006 2:28 pm |
|
|
Quote: | int i;
char a[16],b[16],c[16];
strcpy(a,"ABCDEFGHIJKLMNOP");
strcpy(b,"abcdefghijklmnop");
for(i=0;i<=15;i++)
c[i] = a[i] ^ b[i];
This way it works OK. |
This is not OK. Your strings are 16 characters long, plus the string
terminator byte (0x00) at the end. This is 17 bytes total, but you
arrays are only declared as 16 bytes long. You need to increase
the array size to 17 bytes, minimum. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Aug 18, 2006 6:55 pm |
|
|
Another problem is that a function can not return a pointer to an automatic array. Your temp array is reserved on the stack in RAM, on returning from the function this RAM is given back to the system. You can not count on this memory still being available.
A possible solution is to pass your function an additional parameter with a pointer to a memory buffer for storing the result value.
Code: | void XorBytes(char b1[], char b2[], char c[])
{
int aux,i;
for(i=0;i<=15;i++)
c[i] = b1[i] ^ b2[i];
for(i=0;i<=15;i++)
aux = c[i];
}
void main()
{
int i,aux;
char a[17],b[17],c[17]; // Made c an array as well
strcpy(a,"ABCDEFGHIJKLMNOP");
strcpy(b,"abcdefghijklmnop");
XorBytes(a,b,c);
sleep();
} |
Edit: Changed return type for function XorBytes to void.
Last edited by ckielstra on Sun Aug 20, 2006 7:07 am; edited 1 time in total |
|
|
Guest
|
|
Posted: Sun Aug 20, 2006 3:36 am |
|
|
ckielstra wrote: |
Code: | char XorBytes(char b1[], char b2[], char c[])
{
..........
}
void main()
{
int i,aux;
.........
} |
|
Yep, while this is a solution, I've found myself in one program with a serious problem. The String intended to be a result string has overwritten one of my main function variables destroying everything else. Go figure. How can this be possible? Maybe I have done something wrong but sure this is annoying.
In case the problem is not clearly explained, lets say that variable i declarated in main is 8. Then you call to XorBytes to obtain the desired result. Then, when you're back in main function, you take a look at i and it is anything else. Strange, isn't it? |
|
|
Ttelmah Guest
|
|
Posted: Sun Aug 20, 2006 5:04 am |
|
|
If you know the maximum length of the strings, then you can use:
Code: |
char * XorBytes(char a[],char b[]) {
static char target[17];
int i=0;
do
target[i] = a[i]^b[i];
while (i++<16);
return(target);
}
void main() {
char a[17],b[17],*c;
strcpy(a,"ABCDEFGHIJKLMNOP");
strcpy(b,"abcdefghijklmnop");
c = XorBytes(a,b);
sleep();
}
|
For the really 'flashy' version, you can use the memory management functions, to allocate a dynamic memory block to hold the result.
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Aug 20, 2006 7:19 am |
|
|
Quote: | Yep, while this is a solution, I've found myself in one program with a serious problem. The String intended to be a result string has overwritten one of my main function variables destroying everything else. Go figure. How can this be possible? Maybe I have done something wrong but sure this is annoying. | It sounds like you have an error in your program causing data to be overwritten. Your short demo program shows several of the common causes for these errors:
- Data buffers that are 1 character too short. When copying strings you also have to reserve memory for the terminating zero.
- Returning a string that was allocated on the local heap is a recipe for disaster.
- You are returning a string as a char data type instead of char * (see Ttelmahs code for the correct varible declarations).
Check all your code for the above error types. |
|
|
Guest
|
|
Posted: Sun Aug 20, 2006 10:39 am |
|
|
ckielstra wrote: | It sounds like you have an error in your program causing data to be overwritten. Your short demo program shows several of the common causes for these errors:
- Data buffers that are 1 character too short. When copying strings you also have to reserve memory for the terminating zero.
- Returning a string that was allocated on the local heap is a recipe for disaster.
- You are returning a string as a char data type instead of char * (see Ttelmahs code for the correct varible declarations).
Check all your code for the above error types. |
Hello Ckielstra. I'm sorry for what seems to be a confussion. I'm not the original poster of the thread. I was just adding a comment on something a "trippled" with a few days ago.
I had the char string or buffer declared with the "extra" byte in size into consideration.
On the second statement you had me lost and can't make a comment on it as I don't get what you mean.
And in returning the string I was returning it something like this way (it returned well, but overwrote a different variable declared in main function):
void MyFunction (int iData, char cData/*,...any other info it does need*/ char cResultString[])
{
Here I wrote on it with strcopy and/or with cResultString[i]=any char and added at the end the '\0'
}
Hope it clears things out and thanks so much for your help clarifying this strange happening to us.
Best wishes |
|
|
|
|
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
|