View previous topic :: View next topic |
Author |
Message |
syeda amna
Joined: 28 Dec 2012 Posts: 21
|
string tokens and save in arrays |
Posted: Thu Mar 14, 2013 6:54 am |
|
|
Hi
I want to develop code such that it compares the string separated by "/". What i m missing.
the ouput shows data[0]=0123456789 abd data[1]=Null. why??
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOLVP,PUT,NOPROTECT,BROWNOUT
#use delay(clock=16M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#fuses XT
#include <string.h>
void main()
{
int8 i=0;
int8 x;
char line[40] = {"0123456789/0123456789"}; //sample string
char sep[] = {"/"};
char *data ;
//==========Use strtok() function to to match string========//
data[i] = strtok(line, sep); //It separates this "/"
for(i=1;i<2;i++)
{
data[i] = strtok(0, sep);
}
for(x=0;x<2;x++){
puts(data[x]);
}
if(data[0]==data[1])
{
puts("correct");
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Thu Mar 14, 2013 7:16 am |
|
|
The first search will return the entire string, then the subsequent ones are not searching in the string at all, but in the memory starting at address zero. Not where you are meant to be looking....
Best Wishes |
|
|
syeda amna
Joined: 28 Dec 2012 Posts: 21
|
|
Posted: Thu Mar 14, 2013 11:41 am |
|
|
what will be the solution? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 14, 2013 12:35 pm |
|
|
As a side note, your code below has a serious mistake. First you
specify the HS fuse, which is correct for a 16 MHz crystal. But then
for some strange reason, you add a separate line with the XT fuse
which is for a 4 MHz crystal.
Quote: |
#include <18F452.h>
#fuses HS,NOWDT,NOLVP,PUT,NOPROTECT,BROWNOUT
#use delay(clock=16M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#fuses XT |
How does the compiler handle two different oscillator fuses ?
It uses the last one. See the list of fuses compiled by the compiler
at the end of the .LST file, below. It's using the XT fuse, which will
not work with a 16 MHz crystal. So if you are running real hardware,
the PIC will not run.
Quote: |
Configuration Fuses:
Word 1: 2100 XT NOOSCSEN
Word 2: 0E0E PUT BROWNOUT BORV20 NOWDT WDT128
Word 3: 0100 CCP2C1
Word 4: 0081 STVREN NOLVP NODEBUG
Word 5: C00F NOPROTECT NOCPB NOCPD
Word 6: E00F NOWRT NOWRTC NOWRTB NOWRTD
Word 7: 400F NOEBTR NOEBTRB |
|
|
|
syeda amna
Joined: 28 Dec 2012 Posts: 21
|
|
Posted: Fri Mar 15, 2013 1:37 am |
|
|
thanks PCM Programmer for explaining the fuses.
I want to ask is there any builtin function that can produce the output like this.
Code: |
data[0]=0123456789
data[1]=0123456789
|
I think the code should produce the output like this. but it didnt. the value in data[1] is NULL. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Mar 15, 2013 2:03 am |
|
|
There are several problems.
First, data[i], for i>0 doesn't exist. You are creating a single pointer, not an array....
Then the command passed to strtok, for the later values, should be NULL, not 0 (it may well _be_ zero, but this is not guaranteed - use the correct value).
Then data[0], will never equal data[1] - remember these are string pointers. The first will point to the start of the first string token, the second the second token - not equal. I suspect you want to compare strings, so need to look at strcmp....
So:
Code: |
void main()
{
int8 i=0;
int8 x;
char line[40] = {"0123456789/0123456789"}; //sample string
char sep[] = {"/"};
char data[3] ;
//==========Use strtok() function to to match string========//
data[i] = strtok(line, sep); //It separates this "/"
for(i=1;i<2;i++)
{
data[i] = strtok(NULL, sep);
}
for(x=0;x<2;x++)
{
puts(data[x]);
}
if(strcmp(data[0],data[1])==0)
{
puts("correct");
}
while(TRUE) ; //If you don't do this, the print will never complete as
//the code will fall off the end and go nowhere....
}
|
Best Wishes |
|
|
syeda amna
Joined: 28 Dec 2012 Posts: 21
|
|
Posted: Fri Mar 15, 2013 2:24 am |
|
|
Great!!! Ttelmah
I want to compare and the print the values in data[0] and data[1].
Now how can i print these values?? |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Mar 15, 2013 2:43 am |
|
|
PCM programmer wrote: | As a side note, your code below has a serious mistake. First you
specify the HS fuse, which is correct for a 16 MHz crystal. But then
for some strange reason, you add a separate line with the XT fuse
which is for a 4 MHz crystal.
|
Well -- yes.. I would always make sure to read the datasheet.
As I recall (and again, I would check cause this stuff changes)
HS is for >= 8MHz
XT is for <= 8MHz
But could vary based on which PIC.
otherwise... yes yes on all other accounts. (16MHz and XT do not mix) _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Mar 15, 2013 2:51 am |
|
|
syeda amna wrote: | Great!!! Ttelmah
I want to compare and the print the values in data[0] and data[1].
Now how can i print these values?? |
Er.
The code I have posted, prints them, and compares them.
Best Wishes |
|
|
|