0

I wrote this piece of code

#include <stdio.h>

struct foo
{
    int foo1[3];
};

int main(void)
{
    int a[] = {1, 2, 3};
    struct foo test;
    test.foo1 = a;
    printf("%d\n", test.foo1[0]);

    return 0;
}

It gives compile error saying that it cannot convert int * to int[3].

I know that array names will decay into pointers in expressions, but is there a way of suppressing that since I do need an array here?

Xufeng
  • 6,452
  • 8
  • 24
  • 30

5 Answers5

4

As the others said, there is not direct assignment operators that will copy an array. You have to use memcpy() instead

memcpy(test.foo1, a, sizeof(a));

This is one source of errors in C because the sizeof() needs to be large enough to copy all the data but not too large so as not to overwrite data at tests.foo1. The best practice, I would imagine, is to test that both arrays are the same size before doing the memcpy().

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
1

This is one of the basics of C, arrays cannot be assigned.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 1
    I think *copied* would be a better word. – ajay Feb 23 '14 at 20:25
  • @ajay, better word or not, assignment *is* the technical term in C for that. (Perhaps your are thinking of the C++ copy operator? A different language.) BTW, arrays can be copied when using `memcpy`. – Jens Gustedt Feb 23 '14 at 20:28
  • `int a = 4; int b; b = a;` What I meant by copy is the last statement *copies* the content of `a` into `b`. Or do we say `a` is assigned to `b`? – ajay Feb 23 '14 at 20:43
  • @ajay, the later. In C `=` is the assignment operator. – Jens Gustedt Feb 23 '14 at 22:18
0

In C and in C++ there is no way to assign a value to an whole array. It is also not possible to assign an array the values of an other array (even if the dimension would match).

ROTA
  • 101
  • 3
0

You cannot assign arrays in C. However, you can assign objects of user-defined type, even if those contain an array. So peraps rewrite it like this:

struct foo a = { { 1, 2, 3 } };

struct foo test;
test = a;

Or of course just initialize the object correctly immediately:

struct foo test = { { 1, 2, 3 } };
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

Arrays are not first class objects in C. You can't copy (assign), compare, pass, or return an array. You copy an array into another array element by element. You also compare two arrays element by element. You pass a pointer to a first element of the array and similarly return a pointer to the first element of a dynamically allocated array. Therefore test.foo1 = a; is wrong. You have two choices here.

#include <stdio.h>

struct foo {
    int foo1[3];
};

int main(void) {
    int a[] = {1, 2, 3};
    struct foo test;
    int len = *(&(test.foo1) + 1) - test.foo1; // length of the array test.foo1
    int i = 0;
    for(i = 0; i < len; i++)
        test.foo1[i] = a[i];  // copy the array element-wise

    printf("%d\n", test.foo1[0]);
    return 0;
}

You can also directly copy all the bytes from the array a in main to the array test.foo1 using memcpy.

memcpy(test.foo1, a, sizeof a);

This copies all the bytes of the array a into the array test.foo1. Therefore the array test.foo1 must be large enough else it will lead to undefined behaviour or even segfault.

ajay
  • 9,402
  • 8
  • 44
  • 71