?
Solved

String literal converted to char* in formal argument

Posted on 2003-03-19
1
Medium Priority
?
1,415 Views
Last Modified: 2012-05-04
Hi
I have a small snipet of code that compiles fine in VC++ 6.0 but throws the warning
String literal converted to char* in formal argument , while compiling with Solaris 5.6, CC compiler.
Even though the run time results are correct, I would like to remove this warning

Any suggestions.

#include<iostream>
using namespace std;
class A
{
public:
        void hi(string a){
        cout<<"String "<<a.c_str()<<endl;
        }
        void hi(char* a){
        cout<<"char* "<<a<<endl;
        }
};
int main(int argc, char *argv[])
{
        string a="HAHHAHAHAHH";
        A b;
        b.hi(a);
        b.hi("HOHOHOHOHO");

        return 1;
}


Thanks
Lohit
0
Comment
Question by:lohitverma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
Salte earned 300 total points
ID: 8165935
Just change the type of A::hi() argument to const char * and the warning should go away.

A::hi(char * a) should change to A::hi(const char * a).

#include<iostream>
using namespace std;
class A
{
public:
       void hi(string a){
       cout<<"String "<<a.c_str()<<endl;
       }
       void hi(char* a){ // change here to const char * a.
       cout<<"char* "<<a<<endl;
       }
};

The reason for the warning is that char * means that you are able to modify the argument. A string literal should probably not be modified and so you can't use it as argument to a function that takes 'char *' as argument.

const char * a as argument means "I promise not to modify this string" and such an argument can receive a string literal without problems.

Since your function actually doesn't modify the string there's no reason why it should be 'char *'.

If a data type is 'char *' it will automatically convert to 'const char *' when necessary. The opposite is not true however, so a 'const char *' string can not change to 'char *' unless you use const_cast<> but that should not be necessary in your case.

Alf
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

762 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