Solved

"Implicit conversion of string literal" during compilation

Posted on 2002-07-22
6
715 Views
Last Modified: 2007-12-19
Hi All,

I am beginner of C/C++ programming. Need You help/assist to solve my case, below is some warning messages displayed during compilation BS_LOGIC.c. Currently I am using HPUX 11 64-bit with c compiler and cpp compiler. About the warning messages, should I modify the code or maybe there is a particular option parameters following c/cpp can be used during compilation to make them compatible.

Thank You

Peter

Warning 829: "BS_LOGIC.c", line 465 # Implicit conversion of string literal to
    'char *' is deprecated.
         "declare my_datetime DATE ; old_points number ( 9 ) ; my_trans_n
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 715 # Implicit conversion of string literal to
    'char *' is deprecated.
         "declare my_datetime DATE ; old_points number ( 9 ) ; my_trans_n
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 1191 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "declare my_datetime DATE ; my_trans_no number ( 9 ) ; BEGIN sel
         ^^^^^^^^^^^^^^^^                                                
Warning 829: "BS_LOGIC.c", line 1814 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "declare my_trans_no NUMBER ( 9 ) ; ea_trans_no NUMBER ( 9 ) ; m
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 1829 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "_redeemed , 0 ) + NVL ( points_adjusted , 0 ) + NVL ( points_tr
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 1844 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "ans_no from dual ; insert into event_award_history ( member_no
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 1859 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "_no , datetime_redeem , expiry_date , item_code , merchant_code
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 1874 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "request , datetime_request , transaction_no , event_code , rema
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 2162 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "declare my_trans_no NUMBER ( 9 ) ; my_datetime DATE ; my_points
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 2463 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "declare my_datetime DATE ; my_trans_no number ( 9 ) ; my_member
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 2478 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "where item_code = my_item_code ; update voucher set status = 'C
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 2986 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "declare my_datetime DATE ; my_trans_no number ( 9 ) ; my_old_tr
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 3243 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "declare my_datetime DATE ; my_member_no char ( 10 ) ; my_cso_id
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 3561 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "declare my_trans_no NUMBER ( 9 ) ; ea_trans_no NUMBER ( 9 ) ; m
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 3576 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "ard_points_balance , 0 ) + NVL ( points_redeemed , 0 ) + NVL (
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 3591 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "_points ) ; select ea_transaction_no . nextval into ea_trans_no
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 3909 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "declare my_trans_no NUMBER ( 9 ) ; ea_trans_no NUMBER ( 9 ) ; m
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 3924 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "points , kf_balance_points from members where member_no = :h_me
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
Warning 829: "BS_LOGIC.c", line 4201 # Implicit conversion of string literal
    to 'char *' is deprecated.
         "declare my_trans_no NUMBER ( 9 ) ; my_datetime DATE ; my_event_
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                    
0
Comment
Question by:PeterKarpov
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7170989
I recommend you do modify your code.

If you have code like this:
char * foo = "Hello World";

You should change it to a const type like the following:
const char * foo = "Hello World";
0
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 7170995
Pointers initiated by string literial should be constant type.

If you try to modify a variable that is pointing to a string literial, your code can result in undefined behavior.

On some compilers, you will not get any errors, while on other compilers, you'll get an error.

For example, code compile in VC++ Ver 5.0, has no problem with modifying a variable pointing to a string literial.

However, VC++ Ver 6.0, will give you a runtime error with the same code.
0
 
LVL 1

Expert Comment

by:ris
ID: 7174824
if you need to be able to modify the string, you should use this code instead:

char foo[MAX_LENGTH];
strcpy(foo, "string literal");

or better:

char foo[MAX_LENGTH];
strncpy(foo, "string literal", MAX_LENGTH);

or with dynamic allocation:

int nStrMaxLen = 500;
char foo = new char[nStrMaxLen]; //or you could use malloc()
strncpy(foo, "string literal", nStrMaxLen);

-------------

If you don't need to modify the string at all, then you could considering using #define macros instead of const char* variables as in:

#define foo "string literal"

With the advantage that if you never use 'foo' anywhere in your code, then no memory is ever allocated for it, slightly optimizing your executable code.  The compiler's optimizer may be able to do that just as well with a const char* variable though, so it's more of a matter of personal style than anything else.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 30

Expert Comment

by:Axter
ID: 7174872
>>if you need to be able to modify the string, you should >>use this code instead:

Or if he needs to initialize and declare a string that later needs to be modified, he can do the following:

char foo[] = "Hello World";

or

char foo[MAX_LEN] = "Hello World";


The first method has almost the same effect as the following:
char *foo = "Hello World";

The diffrence being, that you are allowed to modify the char foo[] type.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7378414
Dear PeterKarpov

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "Axter"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7420029
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Fast access to array, using indexes, smart iterative search 2 119
Should CArray be used for a list of pointers in C++? 19 120
computer science syllabus 3 90
max float value 3 53
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question