Simply put, arrays are not assignable. They are a "non-modifiable lvalue". This of course begs the question: why? Please refer to this question for more information:

Why does C++ support memberwise assignment of arrays within structs, but not generally?

Arrays are not pointers. x here does refer to an array, though in many circumstances this "decays" (is implicitly converted) to a pointer to its first element. Likewise, y too is the name of an array, not a pointer.

You can do array assignment within structs:

struct data {
    int arr[10];
};

struct data x = {/* blah */};
struct data y;
y = x;

But you can't do it directly with arrays. Use memcpy.

Answer from user82238 on Stack Overflow
Top answer
1 of 2
9

Why does C not support direct array assignment?

It is arguably a shortcoming, due to missing features around arrays, but one the original designers choose not to resolve.  Add in the (over) emphasis on pointers and pointer arithmetic and keeping track of these things yourself as would be done in assembly.

Arrays in C don't really have sizes.  Yes, you can int A[100]; ... sizeof(A) ... for a declared array, but once A is used in an expression, its type is immediately converted to pointer to element (here int*) and the original array's size is lost.  This is particularly evident with parameters where an array is passed.

There's not even a convenient built-in for length (e.g. say, lengthof) that would work for arrays of any element size — instead we have to write something like (sizeof(A)/sizeof(*A)).

A lot of code in C relies on use of pointers where length is kept by the program using other variables or knowledge rather than having the language keep track.

Often, declaring an array in a struct/union as a last member is secret code for variable length.  See for example:

#include <stdio.h>

int A [100];

extern int B [];

struct foo {
    int E, F;
    int C [];
};


int main()
{
    printf("Hello, %ld, %ld, %ld\n", sizeof A, sizeof *B, sizeof (struct foo) );

    return 0;
}

I can't say for sure but apparently resolving the short comings only for declared arrays but not for pointers seemed not all that useful.  So, some larger built in length mechanism would have been required for general use with pointers.

Still, something could have been done, say by having some syntax that allows the user to specify a number of elements — like A = B[0:n] or A[0:n] = B or some other variant — but they simply stopped short of that, leaving it for memcpy and memmove.  Let's also note that when you choose between memcpy and memmove, with the former you're saying that the source and destination do not overlap, whereas with memmove you're saying they might overlap so backwards copying of the elements may be better than forwards (and it will check at runtime).

So, the various problems:

  • pointer oriented rather than array oriented
  • no implicit size information on pointers
  • aliasing issues
  • arrays can be created without even using any array declaration (e.g. using malloc), so by their nature they have a length the program has to keep track of on its own
    • fixed sized arrays are only useful in narrow contexts

I think punting to the standard library was pretty reasonable here, plus as you note for fixed sized arrays, we can wrap them in a struct/union..

2 of 2
10

C originally came from a predecessor language called B, made by Ken Thompson. In B, there were no types. Everything was a "word" (basically, an int).

In B, arrays were just pointers to the first element. If you declared an array:

auto arr[10];

This would allocate 10 words on the stack (to be freed automatically when the function returned, thus auto), and arr would be a pointer to the first one.

When the type system was added, Dennis Ritchie didn't want any existing code to break. This is why, for example, on early versions of C if you omitted the type it would default to int. It's also where pointer decay (array arguments to functions being just pointers) came from.

For this reason, arrays ended up being a second-class citizen in C for a long time. Because of the stability of the language, even the more modern C standards (like C23 which is supposed to come out this year) have to try to fix it without breaking anything, and this particular issue (copying arrays) is not really a priority, because you rarely actually want to copy an array (especially a large one).

Source: The Development of the C language

Discussions

Simple C array declaration / assignment question - Stack Overflow
In higher level languages I would be able something similar to this example in C and it would be fine. However, when I compile this C example it complains bitterly. How can I assign new arrays to the More on stackoverflow.com
🌐 stackoverflow.com
[Help] Why can't you assign an array to another array?
int b[]; This isn't even legal to begin with, since you didn't give a size for the array. But let's say you did: int a[3] = {0, 1, 2}; int b[3] = {0}; b = a; When you refer to an array's name in almost all contexts, you get a pointer to the array (equivalent to the address of its first element). So this is saying: &(b[0]) = &(a[0]); which is obviously nonsensical, you can't change the address of b[0]. This works: int *b; b = a; because it becomes: b = &(a[0]); and b is a thing you can assign an int* to. They could allow this in contexts where the size of both arrays is known, and make it a shorthand for something like: for (int i = 0; i < min(sizeof(a)/sizeof(a[0]), sizeof(b)/sizeof(b[0])); i++) { b[i] = a[i]; } but the people who created C didn't do that. And because they decay into pointers whenever you pass them anywhere it isn't all that useful. This does work with std::array in C++, since the size information is part of the type and is maintained when you pass references/pointers to such arrays. More on reddit.com
🌐 r/C_Programming
9
2
November 1, 2021
Can’t assign arrays
I have read that arrays cannot be assigned values this way You are not assigning a value to an array. You are assigning values to char objects. Sure, those objects happen to be elements of an array, but that's no problem. Each individual assignment is just an assignment to a char object, not an array object. When people say you can't assign to an array, they mean you can't do something like this: char s[] = "hello"; s = "world"; /* wrong */ Here s is an array, so you cannot assign to s. More on reddit.com
🌐 r/C_Programming
9
0
July 4, 2023
New to C - how do I assign array values to a new array?
Two ways. Define a static number to the array, that will be the amount of items that the array can store: Int c[3]; The other is assign a number at runtime, for that you need to learn memory allocation. More on reddit.com
🌐 r/cprogramming
16
36
October 20, 2021
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_arrays.htm
Arrays in C
Arrays are used to store and manipulate the similar type of data. Suppose we want to store the marks of 10 students and find the average. We declare 10 different variables to store 10 different values as follows − · int a = 50, b = 55, c ...
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Structure-Assignment.html
Structure Assignment (GNU C Language Manual)
structure assignment such as r1 = r2 copies array fields’ contents just as it copies all the other fields.
🌐
Quora
quora.com › How-can-I-declare-and-perform-assignment-to-an-array-of-characters-in-C
How to declare and perform assignment to an array of characters in C - Quora
Answer (1 of 3): I have been to a C/C++ technical interview, and it's worth mentioning the difference between two forms; char* s = “Hello, World!”; char s[14] = “Hello, World!”; The second one allows us to modify the string using indices as desired; s[6] = ‘x'; While the first one ...
Find elsewhere
🌐
Reddit
reddit.com › r/c_programming › [help] why can't you assign an array to another array?
r/C_Programming on Reddit: [Help] Why can't you assign an array to another array?
November 1, 2021 -

The following code snippet has an error error: invalid initializer:

int main() {
  int a[3] = {0, 1, 2};
  int b[3];
  b = a;
  return 0;
}

There were some StackOverflow answers regarding this, but I still have some questions about it.

 

My Understanding

There is the mantra that "arrays decay into pointers", but I'm aware that does not mean that arrays are equivalent to pointers.

Here,

int main() {
  int *p = malloc(3 * sizeof(int));
  for (int i = 0; i < 3; i++)
    p[i] = i;
}

The compiler will allocate a pointer's worth of memory to p, and the value of p is the address of some location on the heap. In the top code snippet, the compiler allocates 3 ints worth of memory and the value of a is that entire block of memory.

So for me, the above code snippet is similar to declaring an int. When declaring some int i, the compiler allocates an int's worth of memory for i and i's value is that block of memory.

And for an int, this would compile just fine:

int main() {
  int x = 5;
  int y;
  y = x;
  return 0;
}

I don't see why the compiler couldn't just take the data stored at a and copy that over to b, just like how it does above.

 


 

I also started messing around with the code some more, and I noticed that this worked for some reason:

int main() {
  int a[3] = {1, 2, 3};
  int *b;
  b = a;
  return 0;
}

What's the difference here?


EDIT: fixed typos

Top answer
1 of 5
6
int b[]; This isn't even legal to begin with, since you didn't give a size for the array. But let's say you did: int a[3] = {0, 1, 2}; int b[3] = {0}; b = a; When you refer to an array's name in almost all contexts, you get a pointer to the array (equivalent to the address of its first element). So this is saying: &(b[0]) = &(a[0]); which is obviously nonsensical, you can't change the address of b[0]. This works: int *b; b = a; because it becomes: b = &(a[0]); and b is a thing you can assign an int* to. They could allow this in contexts where the size of both arrays is known, and make it a shorthand for something like: for (int i = 0; i < min(sizeof(a)/sizeof(a[0]), sizeof(b)/sizeof(b[0])); i++) { b[i] = a[i]; } but the people who created C didn't do that. And because they decay into pointers whenever you pass them anywhere it isn't all that useful. This does work with std::array in C++, since the size information is part of the type and is maintained when you pass references/pointers to such arrays.
2 of 5
2
I don't see why the compiler couldn't just take the data stored at a and copy that over to b, just like how it does above. The simple answer is "that's just not how the language works". There would need to be at least two changes to the language to make this possible. First, the definition of a "modifiable lvalue" would need to be changed. At present, it is explicitly defined to exclude arrays. Second, the "decay to pointer" behaviour would need extra exceptions. The current exceptions are when an array is an operand to the sizeof or unary & operators, or when a string literal is being used in an array initialization. Extra exceptions would need to be added to allow any kind of arrays to be used in an array initialization, and for when an array is used as either operand for a simple assignment operator. There might be other changes necessary; they're just the ones I can think of off the top of my head. I don't know what repercussions it would have on other parts of the language. What's the difference here? The a expression on the right-hand side of the assignment operator yields an array value, but this value undergoes decay to a pointer value. This pointer value is then assigned to the pointer object on the left-hand side of the assignment operator.
🌐
Unstop
unstop.com › home › blog › arrays in c | declare, manipulate & more (+code examples)
Arrays In C | Declare, Manipulate & More (+Code Examples)
March 18, 2024 - Array initialization in C refers to the process of assigning initial values to the elements of an array. It allows you to set the initial state of the array, providing meaningful values that the array will hold during the execution of the program.
🌐
Reddit
reddit.com › r/c_programming › can’t assign arrays
r/C_Programming on Reddit: Can’t assign arrays
July 4, 2023 -

This should be a valid code as taken from CS50:

// Copy string into memory, including '\0'
    for (int i = 0; i <= strlen(s); i++)
    {
        t[i] = s[i];
    }

But in other places (for instance https://www.reddit.com/r/C_Programming/comments/14oolus/comment/jqexig6/?utm_source=share&utm_medium=web2x&context=3), I have read that arrays cannot be assigned values this way. Must be missing something for sure.

🌐
W3Schools
w3schools.com › c › c_arrays.php
C Arrays
C Examples C Real-Life Examples ... C Study Plan C Interview Q&A ... Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value....
🌐
Reddit
reddit.com › r/cprogramming › new to c - how do i assign array values to a new array?
r/cprogramming on Reddit: New to C - how do I assign array values to a new array?
October 20, 2021 - Two ways. Define a static number to the array, that will be the amount of items that the array can store: Int c[3]; The other is assign a number at runtime, for that you need to learn memory allocation.
🌐
cppreference.com
en.cppreference.com › c › language › array
Array declaration - cppreference.com
January 31, 2025 - Objects of array type are not modifiable lvalues, and although their address may be taken, they cannot appear on the left hand side of an assignment operator.
🌐
Simplilearn
simplilearn.com › home › resources › software development › c arrays: types, examples, & advantages
Array in C: Types, Examples, and Advantages Explained
July 31, 2025 - Learn key concepts of arrays in C and how to implement them for storing values. Get practical insights, code examples, and step-by-step guidance in this guide.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Silicon Cloud
silicloud.com › home › c array assignment methods explained
C Array Assignment Methods Explained - Blog - Silicon Cloud
August 4, 2025 - Discover efficient techniques to assign values in C arrays: loops, initializers, memset, and more with code examples.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-arrays
Arrays in C - GeeksforGeeks
We can update the value of array elements at the given index i in a similar way to accessing an element by using the array square brackets [] and assignment operator (=).
Published   October 17, 2025
🌐
O'Reilly
oreilly.com › library › view › c-programming-visual › 0321287630 › 0321287630_ch06lev1sec2.html
Assigning Values to Arrays - C Programming: Visual Quickstart Guide [Book]
October 13, 2004 - You can initialize arrays (assign values to them) in one of two ways. First, you can do it one element at a time. Second, you can assign all of the values when declaring the array (just as you can assign values to any variable when you declare it).
Authors   Larry UllmanMarc Liyanage
Published   2004
Pages   408
🌐
freeCodeCamp
freecodecamp.org › news › how-to-declare-integer-arrays-with-c-programming
Integer Array in C – How to Declare Int Arrays with C Programming
March 13, 2023 - If in an array of five items, you try to access the last element by using an index of 5, the program will run, but the element is not available, and you will get undefined behavior: #include <stdio.h> int main() { int my_numbers[] = {10, 20, 30, 40, 50}; printf("%d\n",my_numbers[5]); return 0; } // output // -463152408 · To change the value of a specific element, specify its index number and, with the assignment operator, =, assign a new value:
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › Arrays_and_strings
C Programming/Arrays and strings - Wikibooks, open books for an open world
August 11, 2003 - You can also initialize as you declare. Just put the initial elements in curly brackets separated by commas as the initial value. If we want to initialize an array with four integers, with 3, 1, 4, and 1 as the initial values: