Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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'
share|improve this question
You don't need to cast the return value of malloc in a C program. – Carl Norum Apr 26 at 15:07

1 Answer

up vote 0 down vote accepted

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 );
        }
    }
 }
share|improve this answer
I know how many i want to create, at the time i need to create this array, i got a int that tells me how many to make. But it is not a constant, i should use the #3 right? – Nick Spot Apr 26 at 14:52
If you know exactly how many you're going to create and it's the same every time, you should use #2. If the number of elements is going to change from one time to the next, then use #3 – K Scott Piel Apr 26 at 14:53
you right. it is not the same everytime, so #3. Gona give it a try. thank you – Nick Spot Apr 26 at 14:55
one last thing. int* a, int * a, and int *a is the same right? – Nick Spot Apr 26 at 14:56
@JohnHarrod Yes, they are the same. I recommend using the int *a; spacing though because int* a, b; is ambiguous. b is not an int *, but an int. (int *a, *b; properly) Also, be sure to free your array when you're done with it! – GRAYgoose124 Apr 26 at 15:34
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.