简体   繁体   English

将RGB值存储在动态多维数组中

[英]Storing RGB values in a dynamic multidimensional array

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. 所以我想知道如何在C中的数组/多维数组中存储多个RGB值,而我不知道有多少个。

I've heard of malloc but I'm not sure of how to use it yet on multidimensional arrays. 我听说过malloc,但不确定如何在多维数组上使用它。

Eg I have n rgb values: 例如,我有n个 rgb值:

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

Thanks! 谢谢!

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. 您不需要多维数组来存储RGB值,只需声明一个具有3个int的结构并动态分配一个结构数组即可存储这些值。

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

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 : 随着其他人的观点,使用RGB实现结构更容易:

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

typedef struct RGB_t RGB;

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. 尽管在c中指针和数组实际上并不完全相同,但为简单起见,它们的行为非常相似。

Stdlib is needed for malloc malloc需要Stdlib

#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: 对于这样的情况,创建一个结构来简化一组颜色的处理也将是一件好事(假设您的颜色是1字节值,因此我们使用char-如果它们可以大于255,则需要使用更大的类型:

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. Malloc将保留您要求的字节数,并返回一个指向内存开始的指针。 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. 因此,您可以使用sizeof()来获取颜色结构的大小(以字节为单位),然后将其乘以所需的值数量。

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. 如果您不这样做,即使您不再具有实际的colors变量,程序也将继续使用该内存-例如,如果它仅存在于函数中。

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: 然后访问将如下所示,因为您每次都将跳过内存中的3个变量:

colors[i*3] = 0;
colors[i*3+1] = 0;
colors[i*3+2] = 0;    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM