Here is my code:
int dim_y, dim_x;
int **map_boundaries;
int **map;
int main( int argc, char* args[] ){
int i;
scanf("%d",&dim_x);
scanf("%d",&dim_y);
map_boundaries = (int **)calloc(dim_y + 40,sizeof(int*));
for(i = 0; i < dim_y + 40; i++){
map_boundaries[i] = (int *)calloc(dim_x + 40,sizeof(int));
}
(*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20];
}
The warning is for the last line, how should the last line be?
When I have it like this it works fine:
#define dim_y 500
#define dim_x 600
int map_boundaries[dim_y+40][dim_x+40];
int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20];
But I want the values of "dim_x" and "dim_y" to be provided by the user and "map" to be global.
First, we need to know what do you want to achieve. There is something obviously wrong with the last line, but without knowing your intent we will only guess how to fix it. Let's analyze your code.
Currently you are dereferencing (uninitialized) (int**)
variable ( (int*)
) and then using it like array - this suggest that you intend map
to be pointer to array of int
s.
You are dynamically allocate 2-dimensional map_boundaries
array with size of ( dim_x+40
) x ( dim_y+40
) - note that valid indexes will be respectively 0... dim_x
+40-1 and 0... dim_y
+40-1.
From your edit I understand that you want to use map_boundaries
as helper to dynamically allocate global map
.
Indeed you can allocate table like like you did. To assign whole array of map_boundaries
to map
you only need to do map = map_boundaries;
like @BLUEPIXY suggested. Then You can make map_boundaries
local.
int dim_y, dim_x;
int **map;
int main( int argc, char* args[] ){
int **map_boundaries;
scanf("%d",&dim_x);
scanf("%d",&dim_y);
map_boundaries = (int**) calloc(dim_y+40, sizeof(int*));
for(int i = 0; i < dim_y+40; i++){
map_boundaries[i] = (int*) calloc(dim_x+40, sizeof(int));
}
map = map_boundaries;
}
Warning occurs because:
(*map)[dim_x+40]
is type of int
(and you obtain it by dereferencing unallocated memory but compiler cannot know that).
(int(*)[dim_x+40])
is (if I'm not mistaken) of type array of pointers to int
( (int*)[]
) - compiler implicitly cast it to int
since you jest cast data into invalid type.
If that's not what you wanted to do, please elaborate on what you actually were trying to achieve, since it is not obvious.
EDIT:
Trimming map_boundaries
to map
(one way to do it):
int trimmed_dim_y_start = 0;
int trimmed_dim_y_size = 20;
int trimmed_dim_x_start = 0;
int trimmed_dim_x_size = 20;
// ...
map = (int**) calloc(trimmed_dim_y_size, sizeof(int*));
for (int i = 0; i < trimmed_dim_y_size; i++) {
map[i] = map_boundaries[ trimmed_dim_y_start + i ] + trimmed_dim_x_start;
}
Note that in this variant you'll have to make map_boundaries
global again since if you don't free all that calloc's you'll get memory leak. Maybe not so bad in this particular program but it's still an important practice to clean up things.
从int(*)中删除括号,然后将所有int *放在括号中。
(*map)[dim_x+40] = ((int*)[dim_x+40])&map_boundaries[20][20];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.