How To Solve Error “Assignment To Expression With Array Type” In C

Error: assignment to expression with array type error

Error: assignment to expression with array type” is a common error related to the unsafe assignment to an array in C. The below explanations can help you know more about the cause of error and solutions.

How does the “error: assignment to expression with array type” happen?

This error happens due to the following reasons:
First, the error happens when attempting to assign an array variable. Technically, the expression (called left-hand expression) before the assignment token (‘=’) has the type of array, and C language does not allow any assignment whose left-hand expression is of array type like that. For example:

#include <stdio.h>
 
int main()
{
    char arr [13];
    arr = "LearnShareIT";
    printf("%s",arr);
    return 0;
}

Or another example:

#include <stdio.h>
 
int main()
{
    int numbers[4];
    numbers = 123;
    return 0;
}

Output: 

error: assignment to expression with array type

The example above shows that the left hand of the assignment is the arr variable, which is of the type array declared above. However, the left type does not have to be an array variable. Instead, it can be a call expression that returns an array, such as a string:

#include <stdio.h>

int main()
{
    "LearnShareIT" = 99999;
    return 0;
}

Output: 

error: assignment to expression with array type

In C, the text that is put between two double quotes will be compiled as an array of characters, not a string type, just like other programming languages.

How to solve the error?

Solution 1: Using array initializer

To solve “error: assignment to expression with array type” (which is because of the array assignment in C) you will have to use the following command to create an array:

type arrayname[N] = {arrayelement1, arrayelement2,..arrayelementN};

For example:

#include <stdio.h>
 
int main()
{
    int array[4] = {1,2,3,4};
    printf("%d",array[2]);
    return 0;
}

Output:

3

Another example:

#include <stdio.h>
 
int main()
{
    char array[13] = "LearnShareIT";
    printf("%s",array);
    return 0;
}

Output:

LearnShareIT

The first example guides you to create an array with 4 given elements with the value 1,2,3,4 respectively. The second example helps you create an array of char type, each char is a letter which is same as in the string in the double quotes. As we have explained, a string in double quotes is actually of array type. Hence, using it has the same effects as using the brackets.

Solution 2: Using a for loop

Sometimes you cannot just initialize an array because it has been already initialized before, with different elements. In this case, you might want to change the elements value in the array, so you should consider using a for loop to change it. 

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char array[13] = "xxxxxxxx";
    char copyarray[13] = "LearnShareIT";

    for (int i = 0; i < 13; i++){
        array[i] = copyarray[i];
    }

    printf("%s",array);
    return 0;
}

Output:

LearnShareIT

Or, if this is an integer array:

#include <stdio.h>
 
int main() {
    int array[4] = {1,2,3,4};
    int copyarray[4] = {5,6,7,8};

    for (int i=0; i<4; i++)
        array[i] = copyarray[i];
 
    for (int i=0; i<4; i++)
        printf("%d", array[i]);
 
    return 0;
}

Output:

5678

When you want to change the array elements, you have to declare a new array representing exactly the result you expect to change. You can create that new array using the array initialiser we guided above. After that, you loop through the elements in that new array to assign them to the elements in the original array you want to change.

Summary

We have learned how to deal with the error “error: assignment to expression with array type” in C. By avoiding array assignment, as long as finding out the reasons causing this problem in our tutorial, you can quickly solve it.

Maybe you are interested:

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *