CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

atol() is making my life hell. HELP

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
oscarjl
Guest







atol() is making my life hell. HELP
PostPosted: Tue Feb 17, 2004 9:15 am     Reply with quote

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

View user's profile Send private message

Re: atol() is making my life hell. HELP
PostPosted: Tue Feb 17, 2004 10:01 am     Reply with quote

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







PostPosted: Tue Feb 17, 2004 10:34 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Feb 17, 2004 2:26 pm     Reply with quote

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







PostPosted: Tue Feb 17, 2004 8:54 pm     Reply with quote

Try

Longtempvalue = atol(&instruction[1]);

instead of

Longtempvalue = atol(instruction[1]);
oscarjl
Guest







PostPosted: Wed Feb 18, 2004 1:54 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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