如何在C中创建一个struct数组?

时间:2015-08-24 15:11:21

标签: c arrays struct roguelike

我正在制作一个roguelike游戏。我想将地图表示为结构数组,例如在数组中有256个结构。地图是一个16 * 16的图块网格,每个图块都有属性,例如是否有一个项目。

所以说我想要一个256个结构tiles的数组:

struct tiles {
        char type; /* e.g. dirt, door, wall, etc... */
        char item; /* item on top of it, if any */
        char enty; /* entity on top of it, e.g. player, orc if any */
}

然后,我需要访问这样的结构数组:

int main(void)
{
        unsigned short int i;
        struct tiles[256];

        for (i = 1; i <= 256; i++) {
                struct tiles[i].type = stuff;
                struct tiles[i].item = morestuff;
                struct tiles[i].enty = evenmorestuff;
        }
}

2 个答案:

答案 0 :(得分:5)

您需要为数组命名。如果int变量如下:

int my_int

int的数组如下:

int my_ints[256]

然后struct tiles的数组如下:

struct tiles my_tiles[256]

答案 1 :(得分:5)

数组是一个变量,就像一个整数,所以你需要给它一个名称来访问它。

注意:数组的索引最低为Ld /Users/ngi-raheelmateen/Library/Developer/Xcode/DerivedData/eQuiz-Sample-gxbnecbwpozhqweupuoifjuubzzx/Build/Products/Debug-iphonesimulator/eQuiz-Sample.app/eQuiz-Sample normal x86_64 cd /Users/ngi-raheelmateen/Documents/EQUIZ-IOS export IPHONEOS_DEPLOYMENT_TARGET=8.4 export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.4.sdk -L/Users/ngi-raheelmateen/Library/Developer/Xcode/DerivedData/eQuiz-Sample-gxbnecbwpozhqweupuoifjuubzzx/Build/Products/Debug-iphonesimulator -F/Users/ngi-raheelmateen/Library/Developer/Xcode/DerivedData/eQuiz-Sample-gxbnecbwpozhqweupuoifjuubzzx/Build/Products/Debug-iphonesimulator -filelist /Users/ngi-raheelmateen/Library/Developer/Xcode/DerivedData/eQuiz-Sample-gxbnecbwpozhqweupuoifjuubzzx/Build/Intermediates/eQuiz-Sample.build/Debug-iphonesimulator/eQuiz-Sample.build/Objects-normal/x86_64/eQuiz-Sample.LinkFileList -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -objc_abi_version -Xlinker 2 -ObjC -lAFNetworking -lAMScrollingNavbar -lDTModelStorage -lDTTableViewManager -lMMDrawerController -framework CoreData -framework CoreGraphics -framework Foundation -framework MobileCoreServices -framework QuartzCore -framework Security -framework SystemConfiguration -framework UIKit -fobjc-arc -fobjc-link-runtime -Xlinker -no_implicit_dylibs -mios-simulator-version-min=8.4 -lz -lPods-eQuiz-Sample -Xlinker -dependency_info -Xlinker /Users/ngi-raheelmateen/Library/Developer/Xcode/DerivedData/eQuiz-Sample-gxbnecbwpozhqweupuoifjuubzzx/Build/Intermediates/eQuiz-Sample.build/Debug-iphonesimulator/eQuiz-Sample.build/Objects-normal/x86_64/eQuiz-Sample_dependency_info.dat -o /Users/ngi-raheelmateen/Library/Developer/Xcode/DerivedData/eQuiz-Sample-gxbnecbwpozhqweupuoifjuubzzx/Build/Products/Debug-iphonesimulator/eQuiz-Sample.app/eQuiz-Sample ,索引最高为0,因此for循环应为:255

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