2

Possible Duplicate:
C++ deprecated conversion from string constant to ‘char*’

I am getting "deprecated conversion from string constant to ‘char*’" error on gcc compiler at 3 places.

When I was compiling it on codeblock, no error was there.

char* decodeCode(char* encodedString)
{
    const char*  decodedString = "";   // ERROR
    char* a = encodedString;
    char store[10000];

    for(int j=0;j <strlen(a);j++)
    {
        if (isdigit(a[j]) || a[j] == '#')
            continue;
        else return "";    //ERROR
    }   
} 

int main()
{
    const char* a;
    a = decodeCode("6999066263304447777077766622337778");   // ERROR
    printf("%s",a);
    return 0;
}

Have you any idea how I could fix it? If so, please write it down clearly (I am newbie...).

Community
  • 1
  • 1
user1590595
  • 795
  • 2
  • 13
  • 37
  • A string literal is of type `const char[]`, which means you cannot modify its contents. Casting to `char *` makes a pointer with no such constraints. Try `const char *`. – obataku Sep 04 '12 at 01:33
  • PS you should stop writing code that manipulates C strings and uses C-style I/O (via `printf)` in favor of the typical C++ approaches, e.g. `std::string` and using `std::cout`. – obataku Sep 04 '12 at 01:34

1 Answers1

5

You need to add const qualifier to declarations of the variable and the function to fix this:

const char* decodeCode(const char* encodedString) {
    ....
    const char* a = encodedString;
    ....
}


Note: The bottom of your function is missing, but make sure that you are not returning store without copying it.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523