2

Getting an odd error trying to initialise an array in C - anyone know why this might happen?

I have a global variable:

static my_type foo[6];

And in an included header file, I have:

typedef uint32_t my_type[5];

I then in a function in the same file as the global variable try to do:

foo = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 6}, {1, 2, 3, 4, 7}, {1, 2, 3, 4, 8}, {1, 2, 3, 4, 9}, {1, 2, 3, 4, 10}};

The compiler (GCC4) gives the error 'expected expression before '{' token'.

Anyone know what's gone wrong and how to fix it?

Cheers!

2 Answers 2

4

That's not initialization, that's assignment. Initialization has to be a single statement:

static my_type foo[6] = {{1, 2, 3, 4, 5},
                         {1, 2, 3, 4, 6},
                         {1, 2, 3, 4, 7},
                         {1, 2, 3, 4, 8},
                         {1, 2, 3, 4, 9},
                         {1, 2, 3, 4, 10}};

You cannot assign to a whole array in C89 with this syntax. What you can do is memcpy from a const:

void initialize_foo()
{
    static const my_type init[6] =
                        {{1, 2, 3, 4, 5},
                         {1, 2, 3, 4, 6},
                         {1, 2, 3, 4, 7},
                         {1, 2, 3, 4, 8},
                         {1, 2, 3, 4, 9},
                         {1, 2, 3, 4, 10}};
    assert(sizeof(foo) == sizeof(init));
    memcpy(foo, init, sizeof(foo));
}
Sign up to request clarification or add additional context in comments.

5 Comments

He does say "in a function" though. Is the issue that he is attempting to initialize a variable other than at its declaration?
Yeah, basically I have a function lower down in the file (my global variables at the top) and I'm trying to set the array to what I wrote in my function. Surely this is possible? Or if not, is there another way of doing what I'd like to do?
I think there might be a GCC extension, but I am not sure. In this case though, you might be best to assign into a temp array, and memcopy... but I dont like that.
So I can only set an array like this at the time I declare the array, and can't then change the contents of the array later? Hmm, that's a bit pants, but okay, thanks!
@ChrisB: I added a memcpy example.
4

If you are under C99:

ISO C99 supports compound literals. A compound literal looks like a cast containing an initializer. Its value is an object of the type specified in the cast, containing the elements specified in the initializer; it is an lvalue. As an extension, GCC supports compound literals in C89 mode and in C++.

But foo must be a pointer

#include <stdio.h>
#include <stdint.h>

typedef uint32_t my_type[5];

int main(void)
{
    int i, j;
    my_type *foo;

    foo = ((my_type[]) {
      {1, 2, 3, 4, 5},
      {1, 2, 3, 4, 6},
      {1, 2, 3, 4, 7},
      {1, 2, 3, 4, 8},
      {1, 2, 3, 4, 9},
      {1, 2, 3, 4, 10}
    });
    for (i = 0; i < 6; i++) {
        for (j = 0; j < 5; j++) {
            printf("%d ", foo[i][j]);
        }
        printf("\n");
    }
    return 0;
}

4 Comments

Are you sure foo = ABC is allowed? foo is an lvalue, but array lvalues are not modifiable.
As per 6.2.5/6 " If the compound literal occurs outside the body of a function, the object has static storage duration; otherwise, it has automatic storage duration associated with the enclosing block." I wouldn't make foo static, the assigned literal only has automatic storage duration; also, what's the benefit of a static variable in main?
You are right (again), there is no benefit in this case, but OP is using static in his example. Anyway edited.
"but OP is using static in his example." Oh, yeah, but the OP also said: "I have a global variable". Then, (s)he tries to assign inside a function. That won't work with compound literals, I fear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.