Don't Do This: Use memcpy
There is a way to do the assignment with a single statement, as long as you are willing to do some preliminary setup to render your code illegible. Your can use the fact that structures can (1) be assigned to each other in one step, and (2) contain fixed-size arrays. The compiler will probably run memcpy
under the hood anyway, but it's a fun exercise in ridiculousness:
#include<stdio.h>
#define SZ 3 // this is just for convenience
// a pointer to an anonymous structure containing our array
typedef struct {
int x[SZ];
} *pthrowaway;
int main(void)
{
int A[SZ]={1,2,3};
int B[4][SZ]={0};
int row_select=2;
pthrowaway a = (pthrowaway)&A;
pthrowaway b = (pthrowaway)&B[row_select];
*b = *a; // magic
return 0;
}
The variables a
and b
are unnecessary. You can actually assign the arrays in a single statement:
*(pthrowaway)&B[row_select] = *(pthrowaway)&A;
Here is an IDEOne link showing the C99 version: https://ideone.com/IQ6ttg
And here is a regular C one: https://ideone.com/pH1hS2