Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I assign arrays as &a = &b?

Tags:

c

I have a problem assigning an array like:

int a[];
int b[] = {1,2,3};
&a = &b;

I know I could use pointers but I want to try it that way...

like image 210
Starfighter911 Avatar asked Oct 24 '11 22:10

Starfighter911


2 Answers

You can't assign arrays in C. You can copy them with the memcpy() function, declared in <string.h>:

int a[3];
int b[] = {1,2,3};

memcpy(&a, &b, sizeof a);
like image 168
caf Avatar answered Sep 19 '22 01:09

caf


That way doesn't work, as you have found. You cannot assign arrays in C.

Structs, however, are assignable. So you can do this:

typedef struct
{
    int x[3];
} T;

T a;
T b = { { 1, 2, 3 } };

a = b;
like image 26
Oliver Charlesworth Avatar answered Sep 19 '22 01:09

Oliver Charlesworth