By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

converting weird #define to const

P: n/a
Hi,

I ran across the following #define in some documentation for an
embedded system (I truncated the actual define, but the general form is
intact).

#define PADDING \
"x-00: ab\r\n"\
"x-01: ab\r\n"\
"x-0F: ab"

and I'd like to convert it to a const. Unfortunately, my C/C++ is a
little rusty (well, maybe a lot rusty) and I'm not quite sure what an
equivalent "const" form might be for this thing. Would something like
the following work?

const char PADDING[] = {
'"','x','-','0','0',':',' ','a','b','\r','\n','"',
'"','x','-','0','1',':',' ','a','b','\r','\n','"',
'"','x','-','0','F',':',' ','a','b','"'
};

Any help would be greatly appreciated.

Thanks,

:Paul

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Paul Erion wrote:
I ran across the following #define in some documentation for an
embedded system (I truncated the actual define, but the general form is
intact).

#define PADDING \
"x-00: ab\r\n"\
"x-01: ab\r\n"\
"x-0F: ab"

and I'd like to convert it to a const. Unfortunately, my C/C++ is a
little rusty (well, maybe a lot rusty) and I'm not quite sure what an
equivalent "const" form might be for this thing. Would something like
the following work?

const char PADDING[] = {
'"','x','-','0','0',':',' ','a','b','\r','\n','"',
'"','x','-','0','1',':',' ','a','b','\r','\n','"',
'"','x','-','0','F',':',' ','a','b','"'
};

Any help would be greatly appreciated.

You don't have to do it one by one character, you may use a literal to
initialise a character array:

const char PADDING[] = "x-00: ab\r\nx-01: ab\r\nx-0F: ab";

besides, you missed the terminator character, and the literal will put
it in.

V
Jul 23 '05 #2

P: n/a

"Paul Erion" <pe****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi,

I ran across the following #define in some documentation for an
embedded system (I truncated the actual define, but the general form is
intact).

#define PADDING \
"x-00: ab\r\n"\
"x-01: ab\r\n"\
"x-0F: ab"

and I'd like to convert it to a const. Unfortunately, my C/C++ is a
little rusty (well, maybe a lot rusty) and I'm not quite sure what an
equivalent "const" form might be for this thing. Would something like
the following work?

const char PADDING[] = {
'"','x','-','0','0',':',' ','a','b','\r','\n','"',
'"','x','-','0','1',':',' ','a','b','\r','\n','"',
'"','x','-','0','F',':',' ','a','b','"'
};


That's the hard way. :-)

Remember that adjacent string literals will be concatenated
at compile time (but an individual literal cannot be broken
between lines)

const char PADDING[] =
{
"blah blah blah"
"yada yada yada"
"etc. etc. etc."
};

or

const char *PADDING =
"blah blah blah"
"yada yada yada"
"etc. etc. etc." ;

In the second case, you might also want to qualify the
pointer itself as const, to prevent inadvertently changing
it to point elsewhere:

const char * const PADDING =
"blah blah blah"
"yada yada yada"
"etc. etc. etc." ;

HTH,
-Mike
Jul 23 '05 #3

P: n/a
Thanks Mike and Victor! I'm (really) glad there is a much simpler way
of constructing that string -- it seems as if I always stumble across
the most tedious method first. And, now knowing that the compiler
concatenates adjacent strings, that #define makes sense (and means that
I was way off base by including the quote character, ", in the const
string).

:Paul

Jul 23 '05 #4

P: n/a
"Paul Erion" <pe****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Thanks Mike and Victor! I'm (really) glad there is a much simpler way
of constructing that string -- it seems as if I always stumble across
the most tedious method first. And, now knowing that the compiler
concatenates adjacent strings, that #define makes sense (and means that
I was way off base by including the quote character, ", in the const
string).


Yes, I thought about that after I posted my reply, but
I supposed you would have figured it out when you saw
the quotes appear in your output. And the lack of a
terminator character ('\0)' (Victor noticed it, I didn't),
could have caused some interesting effects. :-)

-Mike
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.