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

Dumb C question

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



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

Dumb C question
PostPosted: Wed Nov 18, 2009 9:33 am     Reply with quote

I have a constant string #defined:
Code:
#define VERSION_TYPE "BETA"

I can use printf to print this string, no problem. But in another part of the program I want to send only the first character of the string. So far I haven't been able to figure out how to do it... any help, guys?

Compiler is PCWH 4.099, chip is 18F4620, I have "#device PASS_STRINGS=IN_RAM" in the header file. So far I have tried the following, to no avail:
Code:
printf("%C",VERSION_TYPE[0]);
printf("%C",*(&VERSION_TYPE));
printf("%C",VERSION_TYPE);
printf("%C",*VERSION_TYPE);

I don't want to burn a bunch of program space to do this, and it's not a big priority. But I figure there's got to be some simple way of doing this that I'm missing.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Nov 18, 2009 9:44 am     Reply with quote

A #define will replace any occurance of the define with the value during compile. So if you imagine that anywhere you see your define you actually have the "BETA" string instead then you will understand why it doesn't work.

You have several options,

Code:

char str[10];
strcpy(str, VERSION_TYPE);
printf("%c", str[0]);


or use const
Code:

const char version_type[] = "BETA";
printf("%c", version_type[0]);


or split your define
Code:

#define MAJOR_VERSION 1
#define MINOR_VERSION 0
#define VERSION_STR "BETA"
printf("%s %d.%d", VERSION_STR, MAJOR_VERSION, MINOR_VERSION);
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Nov 18, 2009 10:19 am     Reply with quote

Code:

char str[10];
strcpy(str, VERSION_TYPE);
printf("%c", str[0]);

This doesn't work with the original #define. Strcpy() won't take a constant as one of its parameters; it needs two pointers.
Code:

const char version_type[] = "BETA";
printf("%c", version_type[0]);

This did the trick. It allows printing either the entire string, or just the first character or any part of the string.
Code:

#define MAJOR_VERSION 1
#define MINOR_VERSION 0
#define VERSION_STR "BETA"
printf("%s %d.%d", VERSION_STR, MAJOR_VERSION, MINOR_VERSION);

Works great, IF you want to print the entire string and not just the first character. In one place I send the entire version (1.0 BETA, for example). In another I send a shortened version (1.0B). Of course using the const char string works for both, so that's what I did.

Thanks!
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Nov 18, 2009 10:25 am     Reply with quote

dbotkin wrote:

Code:

char str[10];
strcpy(str, VERSION_TYPE);
printf("%c", str[0]);

This doesn't work with the original #define. Strcpy() won't take a constant as one of its parameters; it needs two pointers.


Taken from the manual
"Parameters: dest is a pointer to a RAM array of characters.
src may be either a pointer to a RAM array of characters or it may be a constant string."
"strcpy (string, "Hi There");"

So with a #define
Code:

#define VERSION_TYPE "BETA"

char str[10];
strcpy(str, VERSION_TYPE);
printf("%c", str[0]);

Shuld be exactly the same as

Code:

char str[10];
strcpy(str, "BETA");
printf("%c", str[0]);
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Nov 18, 2009 11:06 am     Reply with quote

Wayne_ wrote:

Taken from the manual
"Parameters: dest is a pointer to a RAM array of characters.
src may be either a pointer to a RAM array of characters or it may be a constant string."
"strcpy (string, "Hi There");"

You're right - it does say that for strcpy(), but not for the rest of the string functions. When I tried it the first time it didn't work. I must have fat-fingered it or something; I just tried it again and it does indeed work.
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