简体   繁体   English

通过函数在C中初始化全局2D数组

[英]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. 如果以后需要重新分配数组,可以在函数内部初始化一个临时的“模板”数组,然后使用memcpy将其内容放入全局数组。

Since it is the 2-d array it should be declared like this data_type array_name [][]; 由于它是data_type array_name [][];数组,因此应像这样声明: data_type array_name [][]; note the you will have to pass some value for last [] bracket. 请注意,您必须为last []括号传递一些值。 ie `float vertices[][3] = _ ; 即`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. 即使全局verticies的类型已更改为指向数组的指针,该代码也会利用您的vertices拼写错误,并使现有代码保持不变。 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: 这段代码可以在Mac OS X 10.9.2 Mavericks上的GCC 4.8.2下使用以下命令行进行干净地编译:

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). 数组old_vertices对应于您对verticies的定义,但已初始化(一次)。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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