ç初始化数组的数组 [英] C initializing array of arrays

查看:151
本文介绍了ç初始化数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题。我需要有浮动数组的数组存储之前,我运行某些功能。我怎样才能实现这个目标,因为我不能初始化withut非恒定的一个数组?我应该做,将创建一个由malloc的数组的函数,然后返回并分配给一个指针?

  typedef结构
{
    浮RGBA [4];
} graphColors;

我需要有grapColors的阵列。我为我的知识缺乏对不起,我是一个Java程序员,需要现在用C的工作。

编辑:

  graphColors * initializeGraphColors(){
    graphColors * GP;
    INT I;
    浮HI = 1.0F;
    浮LO = 0.0;    浮临时[] = {1.0F,1.0F,1.0F,0.0};
    GP =(graphColors *)malloc的(nrOfLines *的sizeof(graphColors));    对于(i = 0; I< nrOfLines;我++){
        GP [I] .RGBA [0] = LO +(浮点)兰特()/((浮点)RAND_MAX /(HI-LO));
        GP [I] .RGBA [1] = LO +(浮点)兰特()/((浮点)RAND_MAX /(HI-LO));
        GP [I] .RGBA [2] = LO +(浮点)兰特()/((浮点)RAND_MAX /(HI-LO));
        gp的[I] .RGBA [3] = 0.0;
    }
    返回GP;
}

然后在我的类:

  graphColors * GC;
GC = initializeGraphColors();

收到此错误:

 错误C2040:'GC':'graphColors *'在间接层面不同于'graphColors


解决方案

如果您需要在阵列中存储在编译时是不知道的价值观,是的,你需要一个功能,虽然你将只需要分配一个数组通过的malloc()如果数组的尺寸在编译时未知的。

所以...如果大小和内容在编译时是已知的,你可以做到这一点...

 的#define NUM_ELEMENTS 2typedef结构
{
    浮RGBA [4];
} graphColors;graphColors阵列[NUM_ELEMENTS] =
{
    {1.0,0.4,0.5,0.6}
    {0.8,0.2,0.0,1.0}
}

如果大小是已知的,但不是值,你可以这样做......

 的#define NUM_ELEMENTS 2typedef结构
{
    浮RGBA [4];
} graphColors;graphColors阵列[NUM_ELEMENTS]无效InitGraph()
{
    的for(int i = 0; I< NUM_ELEMENTS;我++)
    {
        为(中间体J = 0; J&下; 4; J ++)
        {
           数组[I] [J] =的GetValue(I,J);
        }
    }
 }

如果您不知道大小或内容,直到运行时,你可以接近它这样...

  typedef结构
{
    浮RGBA [4];
} graphColors;graphColors *阵列= NULL;无效InitGraph(INT num_elements)
{
    阵列=的malloc(sizeof的(graphColors)* num_elements);    如果(!数组)
    {
       的printf(错误。\\ n);
       出口(-1);
    }    的for(int i = 0; I< num_elements;我++)
    {
        为(中间体J = 0; J&下; 4; J ++)
        {
           数组[I] [J] =的GetValue(I,J);
        }
    }
 }

I have this problem. I need to have an array of float arrays to store before i run some functions. How can i accomplish that, since i cannot initialize an array withut a non constant? Should i make a function that will create that array with malloc then return in and assign to a pointer?

typedef struct
{
    float RGBA[4];
} graphColors;

I need to have an array of grapColors. I'm sorry for my lack of knowledge, im a Java programmer and need to work with C now.

EDIT:

graphColors *initializeGraphColors(){
    graphColors *gp;
    int i;
    float HI = 1.0f;
    float LO = 0.0f;

    float temp[] = {1.0f, 1.0f, 1.0f, 0.0f};
    gp = (graphColors *) malloc(nrOfLines * sizeof(graphColors));

    for(i = 0;i < nrOfLines; i++){
        gp[i].RGBA[0] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
        gp[i].RGBA[1] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
        gp[i].RGBA[2] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
        gp[i].RGBA[3] = 0.0f;
    }
    return gp;
}

Then in my class:

graphColors *gc;
gc = initializeGraphColors();

Getting this error:

error C2040: 'gc' : 'graphColors *' differs in levels of indirection from 'graphColors'

解决方案

If the values you need to store in the array are not known at compile time, yes, you would need a function though you would only have to allocate an array via malloc() if the size of the array is unknown at compile time.

So... if the size and the content is known at compile time, you can do this...

#define NUM_ELEMENTS 2

typedef struct
{
    float RGBA[4];
} graphColors;

graphColors array[NUM_ELEMENTS] =
{
    { 1.0, 0.4, 0.5, 0.6 },
    { 0.8, 0.2, 0.0, 1.0 }
}

If the size is known, but not the values, you can do this...

#define NUM_ELEMENTS 2

typedef struct
{
    float RGBA[4];
} graphColors;

graphColors array[NUM_ELEMENTS];

void InitGraph()
{
    for( int i = 0; i < NUM_ELEMENTS; i++ )
    {
        for( int j = 0; j < 4; j++ )
        {
           array[i][j] = GetValue( i, j );
        }
    }
 }

If you don't know the size or the content until runtime, you can approach it this way...

typedef struct
{
    float RGBA[4];
} graphColors;

graphColors* array = NULL;

void InitGraph( int num_elements )
{
    array = malloc( sizeof( graphColors ) * num_elements );

    if( !array )
    {
       printf( "error\n" );
       exit( -1 );
    }

    for( int i = 0; i < num_elements; i++ )
    {
        for( int j = 0; j < 4; j++ )
        {
           array[i][j] = GetValue( i, j );
        }
    }
 }

这篇关于ç初始化数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆