[GRASS-user] problem with Rast_get_x_value functions
Glynn Clements
glynn at gclements.plus.com
Sat Jul 17 13:17:55 EDT 2010
Jarek Jasiewicz wrote:
> Sorry for GRASS USERS list but I'm totally banned on GRASS-DEV due to my
> proxyserver error, hope I will change it in the next few days, so for
> now I ask here.
>
>
> I have problem with functions
> Rast_get_[c,f,d]_value(void*, RASTER_MAP_TYPE)
>
> according to its description it require void* argument as first so:
>
> suppose in_map_type=CELL_TYPE
>
> Rast_allocate_buf(in_map_type);
> Rast_get_row(in_map_fd, in_buf, r, in_map_type); // for now is OK,
> buffer has proper value
> But next:
> for(c=0;c<ncols;++c)
> internal_map_f[r][c] = Rast_get_f_value(&in_buf[c], in_map_type);
> // not ok, compiler show warning on dereferencing pointer and program
> returns garbage
>
> internal_map_f[r][c] = Rast_get_f_value(&((CELL*)in_buf)[c],
> in_map_type); // OK but I must cast it manually
>
> it is illogical for me, if in_buf (void*) is casted internally to
> (CELL*) why I must to do it again in a function calling?
You cannot dereference a "void*", nor perform arithmetic on one.
If you want to access the map as an array of FCELL values, use
Rast_allocate_f_buf() and Rast_get_f_row(). There is no point in using
the map's native type if you're just going to convert the data anyhow.
If you want to write code which works "natively" with any type, you
need to use G_incr_void_ptr() for array access, e.g.:
size_t size = Rast_cell_size(in_map_type);
...
/* equivalent to "ptr = &in_buf[c]" */
ptr = G_incr_void_ptr(in_buf, c * size);
Most of the time, it's preferable to work with a specific type
internally and convert on input and output.
--
Glynn Clements <glynn at gclements.plus.com>
More information about the grass-user
mailing list