473,856 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_res ult_sql= "select TRADE_DATE , VALUE , "
" from ("
" select to_char(TRADE_D ATE,'yyyymmdd') trade_date ,
THEORETICAL_HIG H_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

Sep 20 '06 #1
6 5152
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_res ult_sql= "select TRADE_DATE , VALUE , "
" from ("
" select to_char(TRADE_D ATE,'yyyymmdd') trade_date ,
THEORETICAL_HIG H_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

Sep 20 '06 #2

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.

Sep 20 '06 #3

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

Sep 20 '06 #4
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
Sep 20 '06 #5
On 20 Sep 2006 04:19:11 -0700, "tropos" <tr**********@h otmail.com>
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_res ult_sql= "select TRADE_DATE , VALUE , "
" from ("
" select to_char(TRADE_D ATE,'yyyymmdd') trade_date ,
THEORETICAL_HIG H_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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 21 '06 #6

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

Sep 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2242
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage? Which of these fundamental language support features could I expect to be absent (and anything else I might have missed): static data zeroing global data zeroing
11
5618
by: Der Andere | last post by:
I want to initialize unsigned int and unsigned char with their maximum value. Just for being sure: Does unsigned int i = pow(256,sizeof(i))-1; unsigned char c = pow(256,sizeof(c))-1; always work? Thanks, Matthias
12
2764
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day trying to get this to work, but been unable to solve my mistake. What the function should return is a file name used for creating logs files. It will look something like JN122345.log
7
53597
by: d.kaufmann | last post by:
i have this part of code: typedef enum { OW_SEARCH_ROM = 0xF0, OW_MATCH_ROM = 0x55, OW_CONDITIONAL_SEARCH_ROM = 0xEC, OW_HANDLE_SELECT = 0x0F } ow_command_t;
2
1725
by: Samuel | last post by:
Imagine you have the following code: try { ... } catch (ThreadAbortException eThread) { if (WorkStopped != null) WorkStopped(this, EventArgs.Empty) }
19
2505
by: Robert Smith | last post by:
I am wondering why it is possible to return a pointer to a string literal (ie. 1) but not an array that has been explicitly allocated. (ie. 2) ? Both would be allocated on the stack, why does the first one not cause a compiler warning? #include <stdio.h> char * funca() { char *a = "blah"; //1 - ok // char a = "blah"; //2 - not ok
48
17009
by: Francine.Neary | last post by:
I've been reflecting on these two types of pointer. As far as I can glean from books, void * and char * are functionally equivalent: the key property of both is that they are pointers that can be faithfully cast to any other pointer type. The only difference seems to be that it is legal to perform arithmetic on a char *, but not on a void *. So if a char * really is just a more functional void *, why would anyone ever use a void * in...
33
15618
by: Michael B Allen | last post by:
Hello, Early on I decided that all text (what most people call "strings" ) in my code would be unsigned char *. The reasoning is that the elements of these arrays are decidedly not signed. In fact, they may not even represent complete characters. At this point I think of text as simple binary blobs. What charset, character encoding and termination they use should not be exposed in the interface used to operate on them. But now I have...
4
3230
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
0
9916
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9762
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10696
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10782
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9531
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7932
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4174
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.