# Thread: Problem: Allocate an array of structs within a struct.

1. ## Problem: Allocate an array of structs within a struct.

I'm trying to create a pixel buffer for a render bucket but I can't get my head around the pointers. Or the declaration.
Can anyone help me sort this out?

// I need to allocate this:
Code:
```struct pixel {	unsigned char R;
unsigned char G;
unsigned char B; };```
in this
Code:
```struct bucket {	long double max;
long double min;
long double factor;
int max_it;
int size;
int xStart;
int yStart;
struct pixel *buffer;
char started;
char done; };```
Access it in another function
Code:
```	bucket->buffer[xl+(yl*bucket->size)].R = map(brightness*brightness, 0, 1000, 0, 155);
bucket->buffer[xl+(yl*bucket->size)].G = map(brightness, 0, 255, 0, 155);
bucket->buffer[xl+(yl*bucket->size)].B = map(brightness, 0, 255, 0, 255);```
So how do I declare and allocate it?
Code:
```int main(int argc, char *argv[])
{
struct bucket bucketlist[buckets_per_frame];
struct pixel* buffer[pixels_per_bucket];

for ( i=0 ; i<buckets_per_frame ; i++ )
{
for ( j=0 ; j<pixels_per_bucket ; j++ )
{
bucketlist[i].buffer[j] = (struct pixel *) malloc(sizeof(struct pixel));
}
}
}``` 2. Code:
```int main()
{
struct bucket bucketlist[buckets_per_frame];

for ( int i=0 ; i<buckets_per_frame ; i++ )
{
bucketlist[i].buffer = malloc(pixels_per_bucket * sizeof(struct pixel));
}

// ...

for ( int i=0 ; i<buckets_per_frame ; i++ )
{
free( bucketlist[i] );
}

return 0;
}``` Popular pages Recent additions array, int, malloc, nested, struct 