View previous topic :: View next topic |
Author |
Message |
oscarjl Guest
|
atol() is making my life hell. HELP |
Posted: Tue Feb 17, 2004 9:15 am |
|
|
I'm having serious problem with the atol() function.
It's basicly not working. I'm maybe doing something wrong, but I'm suspecting something with bankswitch.
Here is some of the code
char instruction[7];
.
.
instruction[6] = 0;
//receives asci from RS232 into instruction
// instruction = "C00100"
Ltempvalue = atol(instruction[1]);
//Ltempvalue = 0!!!!
I stepped in MPLab sim and there I swa that I jumped out on this line first time:
if (c >= '0' && c <= '9')
But I also noted that c wasn't changing it's value after these 2 lines
if(s)
c = s[index++];
What am I doing wrong?
Sorry if this is'nt enough info, I'm kind of snowed in right now...
Really appritiate some help!
Version:
CCS PCM C Compiler, Version 3.181, 16465
Compiler Settings:
Processor: PIC16F874
Pointer Size: 8
ADC Range: 0-1023
Opt Level: 13
Short,Int,Long: 1,8,16 |
|
|
Al
Joined: 13 Nov 2003 Posts: 28 Location: Belfast
|
Re: atol() is making my life hell. HELP |
Posted: Tue Feb 17, 2004 10:01 am |
|
|
Oscarlj,
Can we see more/all of your code as I cannot follow the relationships between your variables here.
One thing which looks suspect however are the follwoing 2 lines:
if(s)
c = s[index++];
The if statement indicates that 's' is a boolean, but the next line indicates it is an array. _________________ Alan Murray |
|
|
oscarjl Guest
|
|
Posted: Tue Feb 17, 2004 10:34 am |
|
|
I think I've found the problem
I had the string "C00100" in "char instruction[7]"
I only wanted the value of the last 6 bytes, so I called atol with this line:
Longtempvalue = atol(instruction[1]);
When I copy the last bytes to a new array and then call atol() like this:
Longtempvalue = atol(newarray);
Then it works. Found it by going through the lst file and comparing the assembly. Yes, I'm pretty proud of that... :-)
Now what is the problem?
I don't directly see how. My guess is it has something to do with passing pointers and restrictions with const.
Anyone brighter than me?
(My guess is, after struggling with this problem all day, any monkey is brighter than me. Not that I'm comparing you to monkeys...)
:-)
BR
Oscar |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Tue Feb 17, 2004 2:26 pm |
|
|
atol() expects a pointer as its argument. instruction[1] is the value in the array, not the address of the array element. To achieve what you what, you need to rewrite the atol() line as follows:
Longtempvalue = atol(instruction +1);
instruction is the address of the first element of the array, and adding 1 to it will point atol() to the second element.
Try it, I tested it and it appears to work. |
|
|
KerryW Guest
|
|
Posted: Tue Feb 17, 2004 8:54 pm |
|
|
Try
Longtempvalue = atol(&instruction[1]);
instead of
Longtempvalue = atol(instruction[1]); |
|
|
oscarjl Guest
|
|
Posted: Wed Feb 18, 2004 1:54 am |
|
|
Wow thanks guys.
That was kind of embarassing wasn't it?
I actually had an discussion with an coworker yesterday where I
explained that instruction[x] = (*(instruction+(x)))
Something should have tipped me off there....
You live you learn.
Thanks Kerry and Charlie!
/Oscar |
|
|
|