Home > database >  Error: expected an expression in C with initializing 1d array
Error: expected an expression in C with initializing 1d array

Time:01-21

I need to initialize array of

array[921600] = {229407, 229407, 229407,...,226851}; 

elements, I allocated array for 921600 elements and initialized array in function:

setArray(array);

and function looks like this:

Void setArray(Uint8 *array){
    array[921600] = {229407, 229407, 229407,...,226851}; //shorten
}

and then I get error in compiler: error: expected an expression. When I initialize elements one by one it works like this:

Void setArray(Uint8 *array)
{
    *(array   0) = (3);
    *(array   1) = (2);
    *(array   2) = (2);
}

CodePudding user response:

In C, you can initialize an array as you declare it [^1]

For example:

int digits[] = { 3, 1, 4, 1, 5, 9, 2, 6 }; // Initialize an array of 8 numbers

However, after an array is created, you cannot use the same syntax to assign it.
Initialization and Assignment are two different operations with different rules.

The closest thing to a bulk assignment I know of is an ugly memcpy using a compound-literal.

memcpy(digits, (int[]){3,1,4,1,5,9,2,6}, sizeof(digits));

Example Code:

#include <stdio.h>
#include <string.h>

int main(void) {
    int digits[8];
    
    memcpy(digits, (int[]){3,1,4,1,5,9,2,6}, sizeof(digits));
    
    for(int i=0; i<8;   i)
    {
        printf("%d", digits[i]);
    }
    
    return 0;
}

But that technique would be extremely difficult (and wasteful) to do on an array of 921600 elements. For a very big array, the data should likely be loaded from a compiled resource or a text file or a similar external source.

[^1]: Except for VLAs. VLAs cannot be initialized this way.

CodePudding user response:

First of all, this:

array[921600] = {229407, 229407, 229407,...,226851} //shorten

doesn't assign the entire array - it's attempting to assign a brace-enclosed list to a single array element (which is a syntax error), and that element is one past the end of the array (if there are 921600 elements in the array, then they are indexed from 0 to 921599).

You cannot assign an entire array's contents using the = operator - if you want to set all elements to the same value you can use memset, otherwise you'll have to assign elements individually.

Your compiler should also be yakking on Void and Uint8 - are you sure you don't mean void (C is case-sensitive) and uint8_t? And if you mean for the elements to be 8 bits unsigned, then they won't be able to represent values like 229407 or 226851.

CodePudding user response:

This doesn't work:

array[921600] = {229407, 229407, 229407,...,226851};

Because you're actually attempting to assign (not initialize) a single value in array, and the {...} syntax is only valid for an initialization.

If you have some fixed set of values you're going to use to set your array, you can place them in another array and copy them in:

void setArray(uint8 *array)
{
    static const int src[921600] = {229407, 229407, 229407,...,226851};
    memcpy(array, src, sizeof src);
}

By making the source array static, you avoid having a large temporary array that could potentially blow up the stack.

  •  Tags:  
  • c
  • Related