Solved

"Implicit conversion of string literal" during compilation

Posted on 2002-07-22
6
705 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now