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 support@ccsinfo.com

Problem with strings.....

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








Problem with strings.....
PostPosted: Mon Jun 29, 2009 3:05 pm     Reply with quote

Hi All,

I've been using CCS C for a while now, but haven't done much with strings. Now, I have a need and can't seem to get a particular function working correctly.

I have a global string variable containing comma separated data (it happens to be NMEA GPS data). I'd like to call a subroutine, and pass as an argument a comma position. The subroutine would then return all the data between the specified comma position, and the next comma position. I think I can write the subroutine parsing code OK, but I'm having trouble figuring out how to pass data to/from the subroutine. At the moment I'm just trying to get a very basic test case working. Here is what I am trying to do:

Code:


char ParseString(int8 NumComma){

   char TempStr[12];

   Switch (NumComma){

      Case '1':
         TempStr[11] = "This is 1";
         break;

      Case '2':
         TempStr[11] = "This is 2";
         break;

      Case '3':
         TempStr[11] = "This is 3";
         break;
   }

   return (TempStr);
}



and

Code:


NMEA_STR[12] = ParseString(1);
fprintf(Console, "%s\n\r", NMEA_STR);

NMEA_STR[12] = ParseString(2);
fprintf(Console, "%s\n\r", NMEA_STR);

NMEA_STR[12] = ParseString(3);
fprintf(Console, "%s\n\r", NMEA_STR);



Right now I'm just passing the comma position to return different string data, but the fprintf statements are printing garbage characters.

Perhaps there is a better way to do what I'm trying to do?

Thanks,

Dave
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 29, 2009 3:14 pm     Reply with quote

You are attempting to return a pointer to a local array.
See this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=38421
Ttelmah
Guest







PostPosted: Mon Jun 29, 2009 3:46 pm     Reply with quote

Also, C, does not support directly loading strings, except in the initialisation of a variable. Strcpy...

Best Wishes
Guest








PostPosted: Sun Jul 05, 2009 6:50 pm     Reply with quote

Hi All,

I made life a lot easier by declaring the temporary string, NMEA_STR[] as a global variable. My parsing routine now accepts a message number, and the routine parses the main RxBuffer[] and returns just the data that is requested in the temporary string. This works great, without a lot of (unnecessary) hurdles!

I see a lot of requests for "parsing" routines for NMEA data, and it's actually quite easy. I just loop thru RxBuffer[] counting comma's (the delimiter for NMEA data) until I reach one less than the message number I'm interested in. I then start putting the following characters into my temporary string until the next comma is found. This is then the data I've requested. Lastly, I append '\0' to the last location of the temporary string to terminate it, and I'm done.

Hehe, I wrote the entire routine in my dentists waiting room before having my teeth cleaned!

Dave
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