![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
dbotkin
Joined: 08 Sep 2003 Posts: 197 Location: Omaha NE USA
|
Dumb C question |
Posted: Wed Nov 18, 2009 9:33 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Nov 18, 2009 9:44 am |
|
|
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);
|
|
|
![](templates/subSilver/images/spacer.gif) |
dbotkin
Joined: 08 Sep 2003 Posts: 197 Location: Omaha NE USA
|
|
Posted: Wed Nov 18, 2009 10:19 am |
|
|
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! |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Nov 18, 2009 10:25 am |
|
|
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]);
|
|
|
![](templates/subSilver/images/spacer.gif) |
dbotkin
Joined: 08 Sep 2003 Posts: 197 Location: Omaha NE USA
|
|
Posted: Wed Nov 18, 2009 11:06 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|