dumb VAXC question about -> operator: what's

Dave P. Schaumann dave at cs.arizona.edu
Wed Mar 13 02:09:06 AEST 1991


In article <1991Mar12.150609.4814 at athena.mit.edu> seaotter at athena.mit.edu (Stace: the Final Frontier) writes:
>
>int init_data(data *p_data)
	       ^^^^^^^^^^^^
>{
>   p_data = (data *) malloc (sizeof (data));
>[...]
>}
>[...]
>	main (int argc, char *argv[])
>	{
>	   /* other variables ... */
>
>	   data *p_data;
>
>	   if (init_data(p_data) == ERROR) {
		         ^^^^^^
>[...]

Your problem is that you don't seem to understand how to do call-by-reference
in C.  In order to get p_data to change in init_data(), you have to pass its
*address*, not just its value.  So you need to change the call to
	init_data(&p_data)

change the declaration to
	int init_data( data **p_data )

and change the initialization to
	*p_data = malloc( sizeof(data) )

You probably also need to re-read the chapter in your text on pointers and
the chapter on function parameters.

-- 
Dave Schaumann | dave at cs.arizona.edu | Short .sig's rule!



More information about the Comp.lang.c mailing list