Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Warning: assigning const char* to char*

PeithonPeithon Posts: 15Member
Hi,

This is a very simple question but I couldn't find it in your FAQ.

I'm using VC++ and compiling a C program, using the /TC flag.

I've got a function for comparing two strings

int strspcmp(const char * s1, const char * s2)
{

char* pS1 = s1;
char* pS2 = s2;
...
...

}

As you can see, I assign the two const ptrs to some non-const ptrs to do some ptr arithmetic,
like incrementing the ptrs to compare the strings.
But I'm getting warning C4090 complaining about the assignment.
I thought I was doing the standard thing for ptr arithmetic. What am I doing wrong?
How do I get rid of the warning?

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : Hi,
    :
    : This is a very simple question but I couldn't find it in your FAQ.
    :
    : I'm using VC++ and compiling a C program, using the /TC flag.
    :
    : I've got a function for comparing two strings
    :
    : int strspcmp(const char * s1, const char * s2)
    : {
    :
    : char* pS1 = s1;
    : char* pS2 = s2;
    : ...
    : ...
    :
    : }
    :
    : As you can see, I assign the two const ptrs to some non-const ptrs
    : to do some ptr arithmetic,
    : like incrementing the ptrs to compare the strings.
    : But I'm getting warning C4090 complaining about the assignment.
    : I thought I was doing the standard thing for ptr arithmetic. What am
    : I doing wrong?
    : How do I get rid of the warning?
    :

    C is just warning you that you defined the parameters as constant. Having a non-constant pointer to it can comprise that constantness.
    If you're really annoyed by the warnings, use an offset variable:
    [code]
    int offset = 0;

    /* Do stuff */
    ...

    /* 'Advance pointer' */
    offset = offset + 12;
    do_something( (s1 + offset) );
    char a_char = *(s1 + offset + 1);
    ...
    [/code]

    Ofcourse, you can also look up the compiler specific options to disable this warning.

    There might be a better way to do this, though this one is the only I can think of.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • PeithonPeithon Posts: 15Member
    : Hi,
    :
    : This is a very simple question but I couldn't find it in your FAQ.
    :
    : I'm using VC++ and compiling a C program, using the /TC flag.
    :
    : I've got a function for comparing two strings
    :
    : int strspcmp(const char * s1, const char * s2)
    : {
    :
    : char* pS1 = s1;
    : char* pS2 = s2;
    : ...
    : ...
    :
    : }
    :
    : As you can see, I assign the two const ptrs to some non-const ptrs
    : to do some ptr arithmetic,
    : like incrementing the ptrs to compare the strings.
    : But I'm getting warning C4090 complaining about the assignment.
    : I thought I was doing the standard thing for ptr arithmetic. What am
    : I doing wrong?
    : How do I get rid of the warning?
    :
    Thanks, but I was being dumb. I've realised that just using const char * instead of char * will kill the warnings and I'll still be able to read string members. In fact, the pass-by-value thing means that I won't even lose track of the original pointers if I use the fn args as they're copies of those pointers.

    Thanks for the offset tip though.

    Regards,

    Chris
  • Mohammad RastMohammad Rast Posts: 29Member
    In VC++6, there is not an Implicit conversion from 'const char*' (not as a parameter of a function but in main() function) to 'char*' and results an error : "error C2440: 'initializing' : cannot convert from 'const char *' to 'char *' "

    EX :

    [code]
    int main()
    {

    char ch1[9] = "Bad";

    const char* ch2 = ch1; // Or = "Bad"

    char* ch3 = ch2; // error

    return 0;
    }
    [/code]


    But you can have an Explicit conversion to 'char*' :


    [code]

    #include
    using namespace std;

    #include // system()


    int main()
    {

    char ch1[9] = "Bad";

    const char* ch2 = ch1; // 'ch2' is a const string that refers to a non-const memory data

    char* ch3 = (char*)ch2; // 'ch3' is a non-const string that refers to a non-const memory data

    ch3[0] = 'M';

    cout << ch3 << endl << ch2 << endl << ch1; // prints three "Mad"s

    system("pause");

    return 0;
    }

    [/code]

    But the following results an operating system error ( "Test.exe has encountered a problem and needs to close. We are sorry for the inconvenience." ), [and there isn't any output] :

    [code]

    #include <iostream>
    using namespace std;

    #include // system()


    int main()
    {

    const char* ch2 = "Bad"; // 'ch2' is a const string that refers to a const memory data

    char* ch3 = (char*)ch2; // OK

    ch3[0] = 'M'; // error : can't change a const memory data

    cout << ch3 << endl << ch2 ;

    system("pause");

    return 0;
    }

    [/code]

    It depends on the referred data, as it can be changed (ch1) or not ("Bad").

    --------------------------------------
    - I don't have Time To waste The Time!
Sign In or Register to comment.