473,320 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

converting weird #define to const

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
4 1532
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

"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Anon Email | last post by:
Hey people, This looks really weird. I can't make sense of it. Is this a mistake? Can anyone help? IStack const & GetStack () const; Cheers,
8
by: G Patel | last post by:
Can people please comment on the layout/style of my problem? The major issue I had was the layout. I ended up having to put a relatively large switch statement, inside an if statement, which is...
24
by: cs_hart | last post by:
I have an application in Java that I would like to port to c++ to integrate with existing c++ app. Is anyone aware of any tools to help? I found microsft has a java->c# convert, but the java app...
13
by: Sam Steingold | last post by:
the following program does not compile with g++ 4.1.1: ==================================================== // $Id$ // $Source$ #include <stdint.h> #include <stdio.h> typedef struct {...
6
by: canoewhiteh2o | last post by:
I am converting a couple of C header files to C#. It is mainly just a bunch C structs but I am not sure how to handle the #ifdef and #ifndef in C#. For example: #ifndef DATE_TIME #define...
8
by: nickyeng | last post by:
I have written 3 files, i dont know whether i do it correctly or wrongly but somehow it compiled well and can run. My simple aim is to display the terrain.txt file into the terrain array, and then...
12
by: cmdolcet69 | last post by:
Can anyone give me some ideas on how to convert the following program to vb 2003? I would like to jsut take this code and implement it in vb. def.h /* Global Type Definitions */ typedef ...
4
by: er | last post by:
hi, the code below generates the following behavior. cld someone please help understand it? 1) clean project + build project generates build errors (see bottom) 2) build a second time, errors...
2
by: cablepuff | last post by:
template <typename ContainerType> ContainerType rsa_encrypt_list(const std::string&, const typename ContainerType::reference, const typename ContainerType::reference); const BigInteger...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.