On Sat, 2008-02-23 at 07:52 +0000, ben lipkowitz wrote:
> On Fri, 22 Feb 2008, Kirk Wallace wrote:
> > Another bit I don't understand:
> >
> >    /* STEP 3: allocate shared memory for joystick data */
> >    js_data = (hal_js_t *) hal_malloc(sizeof(hal_js_t));
> >    if (js_data == 0) {
> >     printf( "ERROR: hal_malloc() failed\n");
> >     hal_exit(comp_id);
> >     return -1;
> >    }
> >
> > hal_js_t is a defined variable type.
> > sizeof(hal_js_t) is the memory size of this type.
> > hal_malloc(sizeof(hal_js_t)) returns the memory location of this
> > variable.
> > I don't understand what (hal_js_t *) does. hal_js_t is a type so it is
> > similar to (int *)?
> 
> hal_malloc returns a (void *) pointer; this would presumably make 
> subsequent code complain about pointer mismatches, and the compiler 
> wouldn't know where to look in the data structure for variables like 
> &(js_data->axis[n]). the (hal_js_t *) casts the returned pointer to the 
> correct pointer type.

Thanks Ben. The cast makes sense. I think of void as being an invention
to allow functions that return nothing, so the function return type
(void *) tells me that a pointer is returned that points to a
nonexistent variable. Is the (void *) just a reminder to always cast the
return pointer to the variable type we happen to need?

-- 
Kirk Wallace (California, USA
http://www.wallacecompany.com/machine_shop/ 
Hardinge HNC lathe,
Bridgeport mill conversion, doing XY now,
Zubal lathe conversion pending)


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Emc-users mailing list
Emc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/emc-users

Reply via email to