DEV Community

Coder Legion
Coder Legion

Posted on • Originally published at coderlegion.com

Assignment makes integer from pointer without a cast in c

#c

Programming can be both rewarding and challenging. You work hard on your code, and just when it seems to be functioning perfectly, an error message pops up on your screen, leaving you frustrated and clueless about what went wrong. One common error that programmers encounter is the "Assignment makes integer from pointer without a cast" error in C.

This error occurs when you try to assign a value from a pointer variable to an integer variable without properly casting it. To fix this error, you need to make sure that you cast the pointer value to the appropriate data type before assigning it to an integer variable. In this article, we will dive deeper into the causes of this error and provide you with solutions to overcome it.

Image description

What makes this error occur?

I will present some cases that triggers that error to occur, and they are all have the same concept, so if you understanded why the failure happens, then you will figure out how to solve all the cases easily.

Case 1: Assignment of a pointer to an integer variable

int main(void)
{
        int *ptr, n1, n2;

        n1 = 2;
        ptr = &n1;
        n2 = ptr; /* Failure in this line */
}
Enter fullscreen mode Exit fullscreen mode

In this simple code we have three variables, an integer pointer "ptr", and two integers "n1" and "n2". We assign 2 to "n1", so far so good, then we assign the address of "n2" to "ptr" which is the suitable storing data type for a pointer, so no problems untill now, till we get to this line "n2 = ptr" when we try to assign "ptr" which is a memory address to "n2" that needs to store an integer data type because it's not a pointer.

Case 2: Returning a Pointer from a Function that Should Return an Integer

int* getinteger() {
    int *intp, myint = 10;

    intp = &myint;
    return intp;
}

int main(void)
{
        int result = getinteger(); /* this line causes the failure */

        return (0);
}
Enter fullscreen mode Exit fullscreen mode

As you can see, it's another situation but it's the same idea which causes the compilation error. We are trying here to assign the return of getinteger function which is a pointer that conatains a memory address to the result variable that is an int type which needs an integer

Case 3: Misusing Array Names as Pointers

int main(void)
{
        int myarray[5] = {1, 2, 3, 4, 5};
        int myint;

        myint = myarray; /* this line will cause the failure */

        return (0);
}
Enter fullscreen mode Exit fullscreen mode

As we might already know, that the identifier (name) of the array is actually a pointer to the array first element memory address, so it's a pointer after all, and assigning a pointer type to int type causes the same compilation error.

The solutions

The key to avoiding the error is understanding that pointers and integers are different types of variables in C. Pointers hold memory addresses, while integers hold numeric values. We can use either casting, dereferencing the pointer or just redesign another solution for the problem we are working on that allows the two types to be the same. It all depending on the situation.

Let's try to solve the above cases:

Case 1: Solution: Deferencing the pointer

We need in this case to asssign an int type to "n2" not a pointer or memory address, so how do we get the value of the variable that the pointer "ptr" pointing to? We get it by deferencing the pointer, so the code after the fix will be like the following:

int main(void)
{
        int *ptr, n1, n2;

        n1 = 2;
        ptr = &n1;
        n2 = *ptr; /* Line fixed */
}
Enter fullscreen mode Exit fullscreen mode

Case 2: Solution: Choosing the right data type

In this case we have two options, either we change the getinteger returning type to int or change the result variable type to a pointer. I will go with the latter option, because there are a lot of functions in the C standard library that returning a pointer, so what we can control is our variable that takes the function return. So the code after the fix will be like the following:

int* getinteger() {
    int *intp, myint = 10;

    intp = &myint;
    return intp;
}

int main(void)
{
        int *result = getinteger(); /* Fixed line */

        return (0);
}
Enter fullscreen mode Exit fullscreen mode

We here changed the result variable from normal int to an int pointer by adding "*".

Case 3: Solution: Using the array subscript operator

In this case we can get the value of any number in the array by using the subscript opeartor ([]) on the array with the index number like: myarray[1] for the second element which is 2. If we still remember that the array identifier is a pointer to the array first memory, then we can also get the value of the array first element by deferencing the array identifier like: *myarray which will get us 1.

But let's solve the case by using the subscript opeartor which is the more obvious way. So the code will be like the following:

int main(void)
{
        int myarray[5] = {1, 2, 3, 4, 5};
        int myint;

        myint = myarray[0]; /* Fixed line */

        return (0);
}
Enter fullscreen mode Exit fullscreen mode

Now the number 1 is assigned to myint without any compilation erros.

The conclusion

In conclusion, the error "assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast" arises in C programming when there is an attempt to assign a memory address (held by a pointer) directly to an integer variable. This is a type mismatch as pointers and integers are fundamentally different types of variables in C.

To avoid or correct this error, programmers need to ensure they are handling pointers and integers appropriately. If the intent is to assign the value pointed by a pointer to an integer, dereferencing should be used. If a function is meant to return an integer, it should not return a pointer. When dealing with arrays, remember that the array name behaves like a pointer to the first element, not an individual element of the array.

Top comments (0)