C:指针的动态数组结构数组 [英] C: Dynamic array of pointers to array of structure

查看:213
本文介绍了C:指针的动态数组结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构,这些结构的二维数组:

  typedef结构{
烧焦exit_n;
焦炭exit_s;
烧焦exit_w;
烧焦exit_e;
}室;房地图[地图宽度] [地图高度]

我需要指针的结构数组。下面code编译,但我没有得到想要的结果。任何帮助吗?这是越来越真糊涂对我来说,任何的解释是AP preciated

 房间*室;
房间=(室*)malloc的(sizeof的(室*)* ROOM_NUM);
[..]
客房[N] =图[room_x] [room_y];
[..]


解决方案

其实,我想你想

 室**室;
房间=(室**)的malloc(sizeof的(室*)* ROOM_NUM);
[..]
客房[N] =&放大器;图[room_x] [room_y];

这给你一个指针数组到你的房间。

I have a structure and a bidimensional array of those structs:

typedef struct {
char exit_n;
char exit_s;
char exit_w;
char exit_e;
} room;

room map[MAP_WIDTH][MAP_HEIGHT];

I need an array of pointers those structs. The following code compiles, but I don't get the wanted result. Any help? This is getting really confused to me, any explanation would be appreciated

room *rooms;
rooms = (room*)malloc(sizeof(room*) * ROOM_NUM);
[..]
rooms[n] = map[room_x][room_y];
[..]

解决方案

Actually, I think you want

room** rooms;
rooms = (room**)malloc(sizeof(room*) * ROOM_NUM);
[..]
rooms[n] = &map[room_x][room_y];

This gives you an array of pointers to your rooms.

这篇关于C:指针的动态数组结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆