从函数初始化C中的全局2D数组

时间:2014-02-28 16:54:49

标签: c arrays multidimensional-array

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

当我编译程序时,我收到此错误:

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

3 个答案:

答案 0 :(得分:4)

您正在使用的语法仅允许初始化;不允许进行作业。

将其移至全局数组的声明将解决问题:

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

如果需要稍后重新分配数组,可以在函数内初始化一个临时“模板”数组,然后使用memcpy将其内容放入全局数组中。

答案 1 :(得分:0)

由于它是2-d数组,因此应该声明为data_type array_name [][];,注意您必须为最后[]括号传递一些值。即`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}};
}

答案 2 :(得分:0)

您无法直接指定数组,因此创建数组复合文字无济于事。但是,结构内部的数组可以通过复合文字指定。因此,您可以考虑使用:

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

代码利用您vertices的拼写错误,并保持现有代码不变,即使全局verticies的类型已更改为指向数组的指针。函数内部的代码使用复合文字来初始化结构,该结构具有初始化指针所指向的数组的(有益的)副作用。

此代码在Mac OS X 10.9.2 Mavericks上使用命令行在GCC 4.8.2下完全编译:

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

我假设在使用和修改数组的某些代码的下一次迭代开始之前,需要初始化函数将数组重置为已知状态。如果您只需要在程序启动时初始化一次数组,那么您可以使用简单的数组初始化程序以不同的方式执行操作。但似乎你已经知道了。

此代码演示了等效性。数组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;
}