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...).

2
  • 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 *. Commented Sep 4, 2012 at 1: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. Commented Sep 4, 2012 at 1:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@user1590595 This compiled fine for me, here is a link to ideone.
yes you are right , i was editing second copy of program instead of first, which i was compiling. now everything is working fine .thank you for kind help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.