C Compiler Logo

Type Mismatch in Assignment

Type Mismatch in Assignment

semantic

warning: assignment makes integer from pointer without a cast

Description

Assigning a value of one type to a variable of an incompatible type.

Common Causes

Example of Error

Error Code

int main() {
    int x;
    char *str = "Hello";
    x = str;
    return 0;
}

Solution

Use proper casting or change the variable type to match the assigned value.

Corrected Code

int main() {
    char *str = "Hello";
    char *x = str;  // Changed x to char*
    // OR if you want the address as an integer:
    // unsigned long x = (unsigned long)str;
    return 0;
}

Additional Tips