简体   繁体   中英

Initializing a global 2D array in C from a function

float verticies[14][3];

init_mod(){

verticies = {{-0.5,-0.5, 0.5},
    { 0.5,-0.5, 0.5},
    {-0.5, 0.5, 0.5},
    { 0.5, 0.5, 0.5},
    {-0.5, 0.5,-0.5},
    { 0.5, 0.5,-0.5},
    {-0.5,-0.5,-0.5},
    { 0.5,-0.5,-0.5},
    { 0.5, 0.5, 0.5},
    { 0.5,-0.5, 0.5},
    {-0.5,-0.5,-0.5},
    {-0.5,-0.5, 0.5},
    {-0.5, 0.5,-0.5},
    {-0.5, 0.5, 0.5}};
}

When I compile the program I get this error:

topsecret.c: In function ‘init_mod’:
topsecret.c:12:14: error: expected expression before ‘{’ token

The syntax that you are using is allowed only for initialization; it is not allowed for assignments.

Moving it to the declaration of your global array will fix the problem:

float verticies[14][3] =
    {{-0.5,-0.5, 0.5},
    { 0.5,-0.5, 0.5},
    {-0.5, 0.5, 0.5},
    { 0.5, 0.5, 0.5},
    {-0.5, 0.5,-0.5},
    { 0.5, 0.5,-0.5},
    {-0.5,-0.5,-0.5},
    { 0.5,-0.5,-0.5},
    { 0.5, 0.5, 0.5},
    { 0.5,-0.5, 0.5},
    {-0.5,-0.5,-0.5},
    {-0.5,-0.5, 0.5},
    {-0.5, 0.5,-0.5},
    {-0.5, 0.5, 0.5}};

If you need to re-assign the array at some later time, you can initialize a temporary "template" array inside your function, and then use memcpy to put its content into the global array.

Since it is the 2-d array it should be declared like this data_type array_name [][]; note the you will have to pass some value for last [] bracket. ie `float vertices[][3] = _ ;

init_mod()
{

    verticies [][3] = {{-0.5,-0.5, 0.5},
    { 0.5,-0.5, 0.5},
    {-0.5, 0.5, 0.5},
    { 0.5, 0.5, 0.5},
    {-0.5, 0.5,-0.5},
    { 0.5, 0.5,-0.5},
    {-0.5,-0.5,-0.5},
    { 0.5,-0.5,-0.5},
    { 0.5, 0.5, 0.5},
    { 0.5,-0.5, 0.5},
    {-0.5,-0.5,-0.5},
    {-0.5,-0.5, 0.5},
    {-0.5, 0.5,-0.5},
    {-0.5, 0.5, 0.5}};
}

You can't directly assign arrays, so creating an array compound literal doesn't help. However, arrays inside structures can be assigned via compound literals. Therefore, you could consider using:

struct FloatArray
{
    float verticies[14][3];
};

struct FloatArray vertices;

float (*verticies)[3] = vertices.verticies;

extern void init_mod(void);

void init_mod(void)
{
    vertices = (struct FloatArray){
        .verticies =
        {
            {-0.5,-0.5, 0.5},
            { 0.5,-0.5, 0.5},
            {-0.5, 0.5, 0.5},
            { 0.5, 0.5, 0.5},
            {-0.5, 0.5,-0.5},
            { 0.5, 0.5,-0.5},
            {-0.5,-0.5,-0.5},
            { 0.5,-0.5,-0.5},
            { 0.5, 0.5, 0.5},
            { 0.5,-0.5, 0.5},
            {-0.5,-0.5,-0.5},
            {-0.5,-0.5, 0.5},
            {-0.5, 0.5,-0.5},
            {-0.5, 0.5, 0.5},
        }
    };
}

The code exploits your misspelling of vertices and leaves your existing code unchanged, even though the type of the global verticies has changed to a pointer to an array. The code inside the function uses a compound literal to initialize the structure, which has the (beneficial) side-effect of initializing the array pointed at by the pointer.

This code compiles cleanly under GCC 4.8.2 on Mac OS X 10.9.2 Mavericks with the command line:

gcc -g -O3 -std=c99 -Wall -Wextra -Werror -c crazy.c

I assume that the initialization function is needed to reset the array to a known state before the start of the next iteration of some code that uses and modifies the array. If you only need the array initialized once when the program starts, then you do things differently, with a simple array initializer. But it seems likely that you already know that.

And this code demonstrates the equivalence. The array old_vertices corresponds to your definition of verticies but is initialized (once).

#include <stdio.h>

float old_vertices[14][3] =
{
    {-0.5,-0.5, 0.5},
    { 0.5,-0.5, 0.5},
    {-0.5, 0.5, 0.5},
    { 0.5, 0.5, 0.5},
    {-0.5, 0.5,-0.5},
    { 0.5, 0.5,-0.5},
    {-0.5,-0.5,-0.5},
    { 0.5,-0.5,-0.5},
    { 0.5, 0.5, 0.5},
    { 0.5,-0.5, 0.5},
    {-0.5,-0.5,-0.5},
    {-0.5,-0.5, 0.5},
    {-0.5, 0.5,-0.5},
    {-0.5, 0.5, 0.5},
};

struct FloatArray
{
    float verticies[14][3];
};

struct FloatArray vertices;

float (*verticies)[3] = vertices.verticies;

extern void init_mod(void);

void init_mod(void)
{
    vertices = (struct FloatArray){
        .verticies =
        {
            {-0.5,-0.5, 0.5},
            { 0.5,-0.5, 0.5},
            {-0.5, 0.5, 0.5},
            { 0.5, 0.5, 0.5},
            {-0.5, 0.5,-0.5},
            { 0.5, 0.5,-0.5},
            {-0.5,-0.5,-0.5},
            { 0.5,-0.5,-0.5},
            { 0.5, 0.5, 0.5},
            { 0.5,-0.5, 0.5},
            {-0.5,-0.5,-0.5},
            {-0.5,-0.5, 0.5},
            {-0.5, 0.5,-0.5},
            {-0.5, 0.5, 0.5},
        }
    };
}

int main(void)
{
    init_mod();

    double old_sum = 0.0;
    double sum = 0.0;
    for (int i = 0; i < 14; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            old_vertices[i][j] *= (i * 14 + j);
            old_sum += old_vertices[i][j];
            verticies[i][j] *= (i * 14 + j);
            sum += verticies[i][j];
        }
    }
    printf("%f == %f\n", old_sum, sum);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM