Thread: warning: deprecated conversion from string constant to 'char*'

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    warning: deprecated conversion from string constant to 'char*'

    Can someone solve this?.
    I have this code which gives me the warning

    warning: deprecated conversion from string constant to 'char*'

    but if i put const in front of the char* inside struct and update the functions it is perfectly fine.

    Can someone tell what's the reason is?

    code without const..

    Code:
    #include <iostream>
    using namespace std;
    #include <string.h>
    
    enum operator_kind {
        OP_plus, OP_minus,
     
    };
    struct NameId {
    	 char *name;
    	int  id;
    };
    static NameId opname[] = {
    	"plus", OP_plus,
    	"minus", OP_minus,
    	NULL,
    };
     char *value2name(NameId *table, int value) ;
     char *op2name(int op);
    
    int main()
    {
          char *p;
          p=op2name(1);
          cout<< p  <<endl;
    }
    char *value2name(NameId *table, int value) 
    {
    	for (int i=0; table[i].name; i++) {
    		if (table[i].id == value)	return table[i].name;
    	}
    	return 0;
    }
    char *op2name(int op)
    {
    	return value2name(opname, op);
    }

    code with const added

    Code:
    #include <iostream>
    using namespace std;
    #include <string.h>
    
    enum operator_kind {
        OP_plus, OP_minus,      
    };
    
    struct NameId {
    	const char *name;
    	int  id;
    };
    
    static NameId opname[] = {
    	"plus", OP_plus,
    	"minus", OP_minus,
    	NULL,
    };
    
    const char *value2name(NameId *table, int value) ;
    const char *op2name(int op);
    
    int main()
    {
      
         const char *p;
          p=op2name(1);
          cout<< p  <<endl;
    }
    const char *value2name(NameId *table, int value) 
    {
    	for (int i=0; table[i].name; i++) {
    		if (table[i].id == value)	return table[i].name;
    	}
    	return 0;
    }
    
    const char *op2name(int op)
    {
    	return value2name(opname, op);
    }
    -Thank you

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i dont see any need for "#include <string.h>", so try and remove that. if you eventually are going to use C++ strings then #include <string> (no ".h"), if you need to use C-style string functions then #include <cstring>.

    does the warning give a line number? what is the command with arguments you use to compile? what compiler are you using?

    i dont get that warning in your first code. i am using the mingw gcc that came with DevC++ (gcc 3.4.2 i think). options used were "-pedantic -Wall -Wextra"

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Thanks for the reply. But removing string.h didn't help. The warning is on line 17.
    End of "};" in the code segment .
    Code:
    static NameId opname[] = {
    	"plus", OP_plus,
    	"minus", OP_minus,
    	NULL,
    };
    I am using g++ 4.2.3 in linux (ubuntu)

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    see the comments in your code. besides those i dont know.
    Code:
    enum operator_kind {
        OP_plus, OP_minus,    /* trailing comma should be removed */
     
    };
    
    static NameId opname[] = {
    	"plus", OP_plus,
    	"minus", OP_minus,
    	NULL,   /* same */
    };

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nadroj View Post
    see the comments in your code. besides those i dont know.
    Code:
    enum operator_kind {
        OP_plus, OP_minus,    /* trailing comma should be removed */
     
    };
    
    static NameId opname[] = {
    	"plus", OP_plus,
    	"minus", OP_minus,
    	NULL,   /* same */
    };
    I'm pretty sure it is valid C or C++ to have a trailing comma.

    I would suggest that you don't put namespace declarations BEFORE header files:
    Code:
    using namespace std;
    #include <string.h>
    That can lead to extremely strange effects and errors that you can't understand.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your code declares a non-const pointer to a string literal. String literals may be stored directly in the read-only portion of a data segment in your exe. By taking a non-const pointer to one of these you're saying that you might try and modify it, but you're not allowed to modify a constant!
    By declaring it as const you're telling the compiler that you just want a pointer to that read-only string literal.

    And yes, it's perfectly okay to have a trailing comma in an enum list. In fact it's good form to do this and have each enum on a seperate line, and that way additions look cleaner to a diff program.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Question:
    Did a book or a teacher teach you to use char *var?
    Do you that alternatives, such as char* var and char * var exist?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I also get the warning that you miss an initializer for NameId::id. Don't know if this affects your code but it is better:
    Code:
    static NameId opname[] = {
    	"plus", OP_plus,
    	"minus", OP_minus,
    	NULL, 0, 
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM