|
|
View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
Messing with pointers |
Posted: Wed Oct 30, 2019 1:05 pm |
|
|
I'm trying to create a function to split strings but I think I'm doing something wrong with the pointers.
Would somebody put some light on this?
I just made this little program to test it.
Code: | #include <18F67J50.h>
#fuses INTRC_PLL,PLL2,NOCPUDIV,NOIESO,NOFCMEN,STVREN,CCP2E7,NOWDT,WDT2048,PROTECT //WDT8Seg.
//$GPRMC Parameters
char UTC_Time[11];
char StatusAV;
char Latitud[11];
char Hemisferio;
char Longitud[11];
char EsteOeste;
char SpeedOverGround[7];
char CourseOverGround[7];
char GPS_Date[7];
#define GPS_RxBufferLen 128
unsigned int8 GPS_RxBufferRMC[GPS_RxBufferLen];
#define splitBufferSize 14
unsigned int8 splitBuffer[17][splitBufferSize];
int1 splitString(unsigned int8 separator,unsigned int16 *xString,unsigned int8 start,unsigned int8 stopChar,unsigned int8 limitLen)
{
unsigned int8 x;
unsigned int8 arrayPos=0;
unsigned int8 xPos=0;
for(x=start;x<=limitLen;x++)
{
if(xString[x]==separator)
{
splitBuffer[arrayPos][xPos]=0;//End of string
arrayPos++;
xPos=0;
}
else if(xString[x]==stopChar)
{
return TRUE;
}
else
{
//Guardar en el arraybuffer
splitBuffer[arrayPos][xPos]=xString[x];
xPos++;
if(xPos>splitBufferSize)
{
return FALSE; //overflow
}
}
}
return TRUE;
}
void main()
{
sprintf(GPS_RxBufferRMC,"$GPRMC,182130.00,A,3532.664333,S,05928.991913,W,0.0,63.1,301019,5.5,W,A*1B\r\n");
while(1)
{
if(splitString(',',&GPS_RxBufferRMC,7,'*',GPS_RxBufferLen))
{
memcpy(UTC_Time,splitBuffer[0][0],6);
StatusAV=splitBuffer[1][0];
memcpy(Latitud,splitBuffer[2][0],11);
Hemisferio=splitBuffer[3][0];//N-S
memcpy(Longitud,splitBuffer[4][0],12);
EsteOeste=splitBuffer[5][0];
strcpy(SpeedOverGround,splitBuffer[6][0]);
strcpy(CourseOverGround,splitBuffer[7][0]);
strcpy(GPS_Date,splitBuffer[8][0]);
}
}
}
|
_________________ Electric Blue |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Wed Oct 30, 2019 1:42 pm |
|
|
I haven't looked too far into your code, but if you are trying to split strings, what about strtok? It is a built-in function that is meant to split up strings based on a delimiter character. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Wed Oct 30, 2019 1:48 pm |
|
|
Check the 'code library' as there's some 'GPS' code there. |
|
|
dexta64
Joined: 19 Feb 2019 Posts: 11
|
Re: Messing with pointers |
Posted: Thu Oct 31, 2019 12:22 am |
|
|
E_Blue wrote: | I'm trying to create a function to split strings but I think I'm doing something wrong with the pointers.
Would somebody put some light on this?
I just made this little program to test it.
Code: |
unsigned int8 GPS_RxBufferRMC[GPS_RxBufferLen];
|
|
The compiler must have given you a pointer mismatch error.
"Pointer types do not match"
Change the variable in this way.
unsigned int16 GPS_RxBufferRMC[GPS_RxBufferLen]; |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Thu Oct 31, 2019 1:19 am |
|
|
Key thing that may be giving issues:
unsigned int16 *xString
That is declaring xString as being a pointer to an int16 array. A 'string' is
an array of characters (so char, byte, or uint8_t). Not int16's.
This declaration means the operations in your splitString function will
be moving forward two characters at a time..... |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Thu Oct 31, 2019 6:24 am |
|
|
Yes! That's it!
I just changed the function to unsigned int8
I have a lot of "Pointer types do not match" and seems to work ok; so I didn't worry.
Any tip about when is really something to take care about and when is just a warning? _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Thu Oct 31, 2019 7:26 am |
|
|
It's tidier to get rid of them, then when the warning does occur you will
know it is a genuine problem like this one.
The reason you are getting this in the other cases will be stuff that uses
'char' or 'byte' instead of 'uint8_t'. The three are equivalent in operation
but the pointer 'checking' knows they are not actually the 'same'.... |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1354
|
|
Posted: Thu Oct 31, 2019 11:00 am |
|
|
E_Blue wrote: | Yes! That's it!
I just changed the function to unsigned int8
I have a lot of "Pointer types do not match" and seems to work ok; so I didn't worry.
Any tip about when is really something to take care about and when is just a warning? |
I'll mostly echo Ttelmah here. Fix the warnings is my suggestion. Put explicit casts. This is inline with more modern C standards (even though CCS is really a K&R 1 variant, they do add new features from ISO standard C versions, and pointer mismatch warnings is one of those newer things they adopted from standard C.
In general it is also good programming practice. By putting that cast there you are showing that you know the types are actually not the same and you are aware of it and your design is ok with that. |
|
|
|
|
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
|