LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2011, 01:30 AM   #1
chinho
Member
 
Registered: Dec 2010
Distribution: fedora, openSUSE
Posts: 49

Rep: Reputation: 1
g++ warning: deprecated conversion from string constant to 'char*'


Hi guys,

My problem is that the warning keeps apperaing when I run g++ compilation.

Say, I have a system runtime function with header (imaginary)sysruntime.h:
void printfunction(char *line);

Then in my .cpp file, I will include the above header
#include <sysruntime.h>

Then, I do the below:
Line12: char *linetoprint = "Print this line";
Line13: printfunction(linetoprint);

But, I will get the warning message "Line12: deprecated conversion from string constant to 'char*'". (Even though it compiles)

My question is how should I declare my char* so that I wont get this warning message, and can still run printfunction(char* line) correctly?

Thks in advance!
Chinho
 
Old 01-10-2011, 01:55 AM   #2
barriehie
Member
 
Registered: Nov 2010
Distribution: Debian Lenny
Posts: 136
Blog Entries: 1

Rep: Reputation: 23
Try:
Code:
const char *linetoprint = "Print this line";
 
1 members found this post helpful.
Old 01-10-2011, 07:35 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Also fix your function to be:
Code:
void printfunction(const char *line);
 
1 members found this post helpful.
Old 01-10-2011, 11:36 PM   #4
chinho
Member
 
Registered: Dec 2010
Distribution: fedora, openSUSE
Posts: 49

Original Poster
Rep: Reputation: 1
Hi guys,

In my real situation, the function i described here is actually part of a system runtime library, so I cant change the function as you adviced.

So I think, the adviced below dont work too:
Code:
const char *linetoprint = "Print this line";
Any other suggestions?

Warmest Regards,
Chinho
 
Old 01-11-2011, 12:04 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
d.cpp:
Code:
#include <stdio.h>
#include "d.h"

int main ()
{
     char *linetoprint = (char*)"Print this line";
     
     printfunction (linetoprint);
     
     return 0;
}

void printfunction (char*d) 
{
   printf ("%s", d);
}
Code:
anisha@linux-uitj:~> g++ d.cpp -Ld.h -Wall -Wextra
anisha@linux-uitj:~>
 
Old 01-11-2011, 03:49 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Code:
const char* linetoprint = "Print this line";

printfunction(const_cast<char*>(linetoprint));
What system library offers printfunction()??
 
Old 01-12-2011, 03:06 AM   #7
chinho
Member
 
Registered: Dec 2010
Distribution: fedora, openSUSE
Posts: 49

Original Poster
Rep: Reputation: 1
Hi anisha, thanks a gazillion!

Hi whitney, none. I was only stating an example
 
1 members found this post helpful.
Old 01-12-2011, 03:09 AM   #8
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by chinho View Post
Hi anisha, thanks a gazillion!
Most welcome, mark the thread solved if you think...it is solved.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
deprecated conversion from string constant to ‘char*’ knobby67 Programming 3 07-03-2010 02:29 PM
Help fixing C++ invalid conversion from const char* to char primerib Programming 26 03-10-2010 06:56 PM
invalid conversion from `const char*' to `char' error holystinken Programming 7 11-23-2009 06:01 PM
error: invalid conversion from const char* to char* Dahakon Programming 1 08-31-2009 09:33 AM
How can I disable the deprecated conversion from string constant to ‘char*’ warning? CelticBlues Programming 4 08-18-2008 12:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration