0

So I was wondering how I could store multiple RGB values, that I don't know how many there are, in an array/multidimensional array in C.

I've heard of malloc but I'm not sure of how to use it yet on multidimensional arrays.

E.g. I have n rgb values:

array[n][3] = {
    array[i] = {ri, gi, bi}, etc...
}

Thanks!

rshah
  • 675
  • 2
  • 12
  • 32
  • Possible duplicate of [Dynamic Multidimensional array in C](http://stackoverflow.com/questions/8725364/dynamic-multidimensional-array-in-c) – jofel Jan 18 '16 at 17:22
  • 7
    For RGB you probably do not need multidimensional array and better go with structs – Vasfed Jan 18 '16 at 17:22

3 Answers3

3

You don't need multidimensional array to store RGB values.Just declare a struct with 3 int's and allocate an array of struct's dynamically to store the values.

#include <stdio.h>
#include <stdlib.h>

struct RGB {
    unsigned char R;
    unsigned char G;
    unsigned char B;
};

int main(void)
{
    //allocate dynamically array of 10 RGB struct's
    struct RGB *rgb = malloc(10*sizeof(struct RGB));
    //white color
    rgb[0].R = 255;
    rgb[0].G = 255;
    rgb[0].B = 255;

    //black color
    rgb[1].R = 0;
    rgb[1].G = 0;
    rgb[1].B = 0;
    /*.............*/
    printf("the R G B of the white color is %d %d %d\n",rgb[0].R,rgb[0].G,rgb[0].B);
    printf("the R G B of the black color is %d %d %d\n",rgb[1].R,rgb[1].G,rgb[1].B);
    //free dynamically allocated memory
    free(rgb);
    return 0;
}
M Oehm
  • 28,726
  • 3
  • 31
  • 42
machine_1
  • 4,266
  • 2
  • 21
  • 42
0

Just use this :

/* N is the number of rows . N here is 3 */

if (( n = malloc( 3*sizeof( int* ))) == NULL )

{ /* error */ }

for ( i = 0; i < 3; i++ )

{
if (( c[i] = malloc( sizeof(int))) == NULL )
{ /* error */ }

/* probably init the row here */
}

/* access matrix elements: n[i] give you a pointer
 * to the row array, n[i][j] indexes an element
*/
n[i][j] = 12;

And as others' point implementing RGB is easier with structures :

struct RGB_t {
    char BLUE;/* I have heared each color is from 0 to 255 */
    char RED;
    char GREEN;
}

typedef struct RGB_t RGB;
  • A `char` may be signed or unsigned, so if you want to store values between 0 and 255 potably , you should use `unsigned char` or `uint_8` from ``.. – M Oehm Jan 18 '16 at 17:48
0

If you want to have an array of a size that is not known at compile time, you are correct to want to allocate the memory dynamically.

In this case, you would actually use a pointer instead of an array. Although pointers and arrays are not actually exactly the same in c, for simple use they act in a very similar way.

Stdlib is needed for malloc

#include <stdlib.h>

It would also be good for a case like this to create a struct to make handling a group of colors simpler (assuming your colors are 1 byte values, so we are using char - if they could be greater than 255, you will need to use a bigger type:

typedef struct color_t {
    unsigned char r;
    unsigned char g;
    unsigned char b;
} color;

Then you can allocate the memory as follows:

int n = 200; //However many there are, you will need to know this before allocating memory
color* colors = (color*) malloc(sizeof(color)*n);

Malloc will reserve as many bytes as you ask it for, and return a pointer to the start of the memory. So, you use sizeof() to get the size in bytes of the color structure, and then multiply that by the amount of values you need.

You can then address the structure in memory using

int i = 20; //Where i is the particular color you want to access
colors[i].r = 0;
colors[i].g = 0;
colors[i].b = 0;

When you are done with the memory, you need to free it using

free(colors);

If you don't do this, the program will continue using this memory even if you no longer have the actual colors variable - for example, if it only existed inside a function.

If you did it without a struct, allocating would look like this:

unsigned char* colors = (unsigned char*) malloc(sizeof(unsigned char)*n*3);

And then accessing would look like this, as you would be skipping through 3 variables in memory each time:

colors[i*3] = 0;
colors[i*3+1] = 0;
colors[i*3+2] = 0;    
davedavedave
  • 487
  • 6
  • 11