I have such structures:
typedef struct s_points
{
double x;
double y;
double z;
int color;
} t_points;
typedef struct s_map
{
int width;
int height;
t_points ***points;
} t_map;
I want to read and store an 2D array inside my ***points;
, but i don't know how to allocate it properly.
Here is my code(in map->width and map->height i store width and height of input array):
t_map *validate(int fd, char *av)
{
int lines;
int j;
char *tmp;
t_map *map;
t_points **tmp_p;
j = 0;
if (!(map = (t_map*)malloc(sizeof(t_map))))
error("ERROR: malloc error");
if ((!(map->points = (t_points***)malloc(sizeof(t_points**) * map->height)))
error("ERRROR!");
while((get_next_line(fd, &tmp)) > 0) //get_next_line will read input line by line
{
if (!(map->points[j] = (t_points**)malloc(map->width * sizeof(t_points*))))
error("ERROR: malloc error");
/* Some other code */;
j++;
}
return(map);
}
It works, but when I trying to write something inside map->points[x][y];
I have segfault, so, as i understand, I have done mistake in memory allocation. So I can't understand how to do it in right way.