converting weird #define to const

P

Paul Erion

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
 
V

Victor Bazarov

Paul said:
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
 
M

Mike Wahler

Paul Erion said:
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
 
P

Paul Erion

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
 
M

Mike Wahler

Paul Erion said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top