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
-Thank youCode:#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); }



LinkBack URL
About LinkBacks



