简体   繁体   English

如何访问用户定义的数据类型中的数组元素? (C)

[英]How to access array elements in a user defined data type? (C)

I am writing a C program to emulate background mode 7 of the SNES. 我正在编写一个C程序来模拟SNES的后台模式7。 I need r (red), g (green) and b (blue) values for the pixel to display at each "dot" (pixel/point) on the screen. 我需要在屏幕上的每个“点”(像素/点)处显示像素的r(红色),g(绿色)和b(蓝色)值。 This requires a transformation of a "tilemap" to get (The tilemap is simply a 1024 x 1024 grid of pixels that you use to decide what to place on the 256 x 224 pixel display). 这需要将“tilemap”转换为get(tilemap只是一个1024 x 1024像素网格,用于决定在256 x 224像素显示屏上放置什么)。 I need to be able to read from and write to pixels as their individual r, g and b values on the tilemap. 我需要能够在tilemap上读取和写入像素作为它们各自的r,g和b值。

To quickly explain the format that I'm using there are 256 "characters" which are just 8 x 8 pixel blocks (composed of three colour bytes to represent red, green and blue). 为了快速解释我正在使用的格式,有256个“字符”,它们只是8 x 8像素块(由三个颜色字节组成,代表红色,绿色和蓝色)。 The tilemap is made up of a 128 x 128 grid of any of these characters. tilemap由任何这些字符的128 x 128网格组成。 Once the tilemap is constructed, a transformation is preformed on the tilemap to discern the individual pixels to display on a 256 x 224 pixel screen. 构建tilemap后,将在tilemap上执行转换,以识别要在256 x 224像素屏幕上显示的各个像素。

To achieve this I am defining: a struct named "colour" containing three unsigned chars for the r, g and b bytes, a data type named "CHR" which is an 8 x 8 array of "colour"s, a data type named "ChrRom" which is an array of 256 "CHR"s (probably could avoid being defined but it was the only way I could think to actually have an array of arrays that I can also dereference... I think). 为了达到这个目的,我定义了一个名为“color”的结构,包含r,g和b字节的三个无符号字符,一个名为“CHR”的数据类型,它是一个8 x 8的“颜色”数组,一个名为的数据类型“ChrRom”是一个256“CHR”的数组(可能可以避免被定义,但这是我能想到的唯一方法,实际上有一个阵列数组,我也可以解除引用...我认为)。 Finally, I have a dereferenced array named "Tilemap" to (hopefully) store the memory addresses of each element in the "ChrRom" struct. 最后,我有一个名为“Tilemap”的解引用数组(希望)存储“ChrRom”结构中每个元素的内存地址。 I am sorry if that wasn't clear but here's the example code: 如果不清楚我很抱歉,但这是示例代码:

typedef struct{
    unsigned char r;
    unsigned char g;
    unsigned char b;
} colour;

typedef colour CHR[8][8];

typedef CHR ChrRom[256];

int main() {
    ChrRom *Tilemap[128][128];
    //Do other computation
    return 0;
};

So far, this will compile. 到目前为止,这将编译。 However, I now want to set the colour values of a certain pixel in a character and immediately there is a problem; 但是,我现在想要设置角色中某个像素的颜色值,并立即出现问题; firstly, I cannot access the individual colours of a pixel in a "character" anymore. 首先,我无法再访问“角色”中像素的各个颜色。 I only have access to the tilemap itself, secondly, I do not know how to read the colour values in the tilemap anyway. 我只能访问tilemap本身,其次,我不知道如何读取tilemap中的颜色值。 On the topic of the second problem (assume for a moment that I somehow read from a file and piped the appropriate colour values into the tilemap characters and composed the tilemap of said characters in a given order), I tried: 关于第二个问题的主题(假设我以某种方式从文件中读取并将适当的颜色值传送到tilemap字符并按给定顺序组成所述字符的tilemap),我尝试:

//Still in the function where Tilemap was declared obviously (main)
printf("%hhu\n", Tilemap[1][1].ChrRom[7].CHR[1][3].r);

GCC will not compile this. 海湾合作委员会不会编译这个。 It says: 它说:

request for member 'ChrRom' in something not a structure or union 请求成员'ChrRom'不是结构或联合

but if I remove ChrRom and CHR before the array entries, I get: 但如果我在数组条目之前删除ChrRom和CHR,我得到:

expected identifier before '[' token '''令牌之前的预期标识符

Finally, I have a dereferenced array named "Tilemap" to (hopefully) store the memory addresses of each element in the "ChrRom" struct. 最后,我有一个名为“Tilemap”的解引用数组(希望)存储“ChrRom”结构中每个元素的内存地址。

No, by defining ChrRom *Tilemap[128][128] you declare to store the memory addresses of ChrRom s (of which there exists only one). 不,通过定义ChrRom *Tilemap[128][128]您声明存储ChrRom的内存地址(其中只存在一个)。 To store the memory addresses of elements in the "ChrRom" struct, you have to specify the type of those elements: CHR *Tilemap[128][128] . 要在“ChrRom”结构中存储元素的内存地址,必须指定这些元素的类型: CHR *Tilemap[128][128]

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

相关问题 用户定义的数组元素和C中的数组大小 - User defined array elements and array size in C 如何检查数组C中元素的数据类型 - How to check the data type of elements in an array C 在C中创建和初始化用户定义类型的数组 - Creating and initializing an array of user defined type in C 在C中初始化用户定义的类型数组 - Initializing user defined type array in C const说明符,用于C中用户定义的数据类型 - const specifier to the user defined data type in C 用户如何在为 100 个元素定义的数组中输入 10 个元素? - How can a user enter 10 elements in an array defined for 100 elements? 如何在C中创建用户定义的结构数组 - How to make a user defined array of struct in C 如何在不使用任何用户定义的数据类型的情况下,在C或C ++中构建大小为n的全新数据类型? - How to build completely new data type of size n in C or C++ without using any of the user defined data type? 如何在没有警告的情况下传递用户定义的固定长度数组类型(C和OpenCL) - how to pass user-defined fixed length array type without warning ( C and OpenCL ) 如何访问在 c 中声明的用户定义数据类型(共享库) - How to access user defined data types declared in .so (shared libraries) in c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM