Search code examples
c++space

I Keep getting this warning when trying to submit my assignment warning: comparison with string literal results in unspecified behaviour [-Waddress]


What Really gets me upset is that this works on Visual Basic and not on my crappy school's server :(.

Note Mdestination and Mname are pointers

    if (Mdestination != nullptr && Mname != nullptr && Mname != "" && Mdestination != "") {
        strcpy_s(name, Mname);
        strcpy_s(destination, Mdestination);

    }


}

Solution

  • I believe here you are mixing pointer arithmetic with string operations.

    Mname != ""
    

    Above line checks whether the pointer Mname and the literal "" are not at the same address. I believe your intent is to check whether the Mname does not point to an empty string. In that case, you could use

    *Mname != '\0' // NUL could also be used instead of '\0'
    

    Same goes with Mdestination.