Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to restructure old C initialisation of char* to avoid compiler warnings

Reply
Thread Tools

How to restructure old C initialisation of char* to avoid compiler warnings

 
 
tropos
Guest
Posts: n/a
 
      09-20-2006
For my sins, I'm maintaining some old C code which is migrated to C++.
Dozens of lines of it looks like this:

char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , "
" from ("
" select to_char(TRADE_DATE,'yyyymmdd') trade_date ,
THEORETICAL_HIGH_VALUE"
" from %s "
" where trade_date = (select max(trade_date ) from %s where "
<snip>
;


The Solaris native C++ compiler complains a lot, saying "Warning:
String literal converted to char* in initialization." Even if I
convert the "char *" to "const char *", a lot of the continuation lines
get that warning. I'd like to get rid of the warnings.

Being a nice C++ coder, I find a line like

char * s1 = "something " "and something else" "and more" ;

pretty wierd. Is this even legitimate C?
Is there any way to concatenate C strings, upon initialisation of a
char *, that won't reap a warning?

Thanks

Tropos

 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      09-20-2006
tropos wrote:
> For my sins, I'm maintaining some old C code which is migrated to C++.
> Dozens of lines of it looks like this:
>
> char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , "
> " from ("
> " select to_char(TRADE_DATE,'yyyymmdd') trade_date ,
> THEORETICAL_HIGH_VALUE"
> " from %s "
> " where trade_date = (select max(trade_date ) from %s where "
> <snip>
> ;
>
>
> The Solaris native C++ compiler complains a lot, saying "Warning:
> String literal converted to char* in initialization." Even if I
> convert the "char *" to "const char *", a lot of the continuation lines
> get that warning. I'd like to get rid of the warnings.
>
> Being a nice C++ coder, I find a line like
>
> char * s1 = "something " "and something else" "and more" ;
>
> pretty wierd. Is this even legitimate C?
> Is there any way to concatenate C strings, upon initialisation of a
> char *, that won't reap a warning?


Change the char * declarations to implicitly-sized character arrays. In
other words, replace:

char * s1 = "something " "and something else" "and more" ;

with

char s1[] = "something " "and something else" "and more" ;

That should eliminate the warnings because an array declaration
effectively allocates writable storage. In other words, a char array is
suitable for initialization by a constant string literal, whereas a
char pointer (with no allocated, writable storage of its own) is not.

Greg

 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      09-20-2006

Greg wrote:
> Change the char * declarations to implicitly-sized character arrays. In
> other words, replace:
>
> char * s1 = "something " "and something else" "and more" ;
>
> with
>
> char s1[] = "something " "and something else" "and more" ;
>
> That should eliminate the warnings because an array declaration
> effectively allocates writable storage. In other words, a char array is
> suitable for initialization by a constant string literal, whereas a
> char pointer (with no allocated, writable storage of its own) is not.
>
> Greg


Beware though what happens afterwards. If you are returning these
pointers from functions, for example, you will now be returning local
stack.

I don't know why you are getting the warnings by putting const in. That
should fix it.
The concatenations are perfectly valid in C++ too and have nothing to
do with the warnings. Such a trick with concatenations is useful when
you have a long literal string that you want to break over lines.

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      09-20-2006

Earl Purple wrote:
> Greg wrote:
> > Change the char * declarations to implicitly-sized character arrays. In
> > other words, replace:
> >
> > char * s1 = "something " "and something else" "and more" ;
> >
> > with
> >
> > char s1[] = "something " "and something else" "and more" ;
> >
> > That should eliminate the warnings because an array declaration
> > effectively allocates writable storage. In other words, a char array is
> > suitable for initialization by a constant string literal, whereas a
> > char pointer (with no allocated, writable storage of its own) is not.
> >
> > Greg

>
> Beware though what happens afterwards. If you are returning these
> pointers from functions, for example, you will now be returning local
> stack.


That is a good point, I had assumed that these were global string
declarations.

> I don't know why you are getting the warnings by putting const in. That
> should fix it.
> The concatenations are perfectly valid in C++ too and have nothing to
> do with the warnings. Such a trick with concatenations is useful when
> you have a long literal string that you want to break over lines.


I believe the issue with the concatenation is that only "adjacent"
string literals are to be combined. Apparently strings literals not on
the same line should not be considered adjacent to each other.

Nonetheless, it seems that most, if not all, C++ compilers do
concatenate strings on separate lines, but at least some have
deprecated the feature (hence the warnings). At any rate, adding a
backslash to the end of each line that evokes a warning will combine
the line with the one following - essentially creating one very long
line from the compiler's point of view.

Greg

 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-20-2006
tropos posted:

> Being a nice C++ coder, I find a line like
>
> char * s1 = "something " "and something else" "and more" ;
>
> pretty wierd.



It's used all the time. Two string literals place side by side become one;
the initialisation above is equivalent to:

char *s1 = "something and something else and more";


> Is this even legitimate C?



Legal C89. Legal C99. Legal C++.


> Is there any way to concatenate C strings, upon initialisation of a
> char *, that won't reap a warning?



Just disable the warning. Depending on how "clever" the code is, you might
run into problems if you change the pointers to "pointer to const".

--

Frederick Gotham
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-21-2006
On 20 Sep 2006 04:19:11 -0700, "tropos" <(E-Mail Removed)>
wrote in comp.lang.c++:

> For my sins, I'm maintaining some old C code which is migrated to C++.
> Dozens of lines of it looks like this:
>
> char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , "
> " from ("
> " select to_char(TRADE_DATE,'yyyymmdd') trade_date ,
> THEORETICAL_HIGH_VALUE"
> " from %s "
> " where trade_date = (select max(trade_date ) from %s where "
> <snip>
> ;
>
>
> The Solaris native C++ compiler complains a lot, saying "Warning:
> String literal converted to char* in initialization." Even if I
> convert the "char *" to "const char *", a lot of the continuation lines
> get that warning. I'd like to get rid of the warnings.
>
> Being a nice C++ coder, I find a line like
>
> char * s1 = "something " "and something else" "and more" ;
>
> pretty wierd. Is this even legitimate C?
> Is there any way to concatenate C strings, upon initialisation of a
> char *, that won't reap a warning?
>
> Thanks
>
> Tropos


If your C++ compiler issues that message on this:

const char *c1 = "Hello, " "World";

....it is broken.

If your C++ compiler does not issue that message on the above, but it
does issue that message on this:

const char *c1 = "H"
"e"
"l"
"l"
"o"
","
" "
"W"
"o"
"r"
"l"
"d";

....then it is still broken.

According to the standard, both of these should appear to the
"compiler" as:

const char *c1 = "Hello, World"

In translation phase 6, adjacent string literals are concatenated. It
isn't until translation phase 7 that preprocessing tokens, which
include keywords like "const" and "char", are analyzed syntactically
and semantically.

So if you modify the code to look like the first or second examples,
and you get that message from the compiler, your compiler is
non-conforming. Are you sure there isn't something different about
the continuation lines that "get that warning" even after you change
the type of the pointer to const char *?

Why not copy and post one that generates the warning and one that does
not?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      09-21-2006

Greg wrote:

> Earl Purple wrote:
> > Greg wrote:
> > I don't know why you are getting the warnings by putting const in. That
> > should fix it.
> > The concatenations are perfectly valid in C++ too and have nothing to
> > do with the warnings. Such a trick with concatenations is useful when
> > you have a long literal string that you want to break over lines.

>
> I believe the issue with the concatenation is that only "adjacent"
> string literals are to be combined. Apparently strings literals not on
> the same line should not be considered adjacent to each other.


Who said that?

#include <string>
int main()
{
std::string s = "this is split"
"over two lines";
}

is perfectly correct. The two string literals are adjacent and are
concatenated.

Gavin Deane

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Restructure dictionary (Python 2.5) Fencer Python 2 03-02-2009 03:21 PM
Initialisation of reference vs. initialisation of reference member Tim Clacy C++ 8 05-30-2006 06:14 PM
From the Tutorial Wiki: suggested restructure Gerard Flanagan Python 0 04-30-2006 09:44 PM
"Restructure a Windows application" from scratch in 2 months Rajat Tandon ASP .Net Web Services 5 01-20-2005 09:43 PM



Advertisments