Simple ptr passing question

Xiang-Min Wang xwang at gmuvax2.gmu.edu
Thu May 23 06:41:53 AEST 1991


In article <24268 at unix.SRI.COM> ric at ace.sri.com (Richard Steinberger) writes:
>
>	I am a bit rusty with C.  Could someone help me with this simple
>pointer passing problem.  I wanted to pass a ptr to a function and
>have the function allocate some space and pass back the address in the ptr.
>Here's what I tried:
>
>#include <stdio.h>
>main()
>{
>  extern void zip();
>  int *ip;
>
>  zip(ip);
>
>  printf("*ip is %d\n",*ip);
>}
>
>void zip (iptr)
>int *iptr;
>{
>  int * jptr;
>  jptr = (int *) malloc(sizeof (int) );
>  *jptr = 12;
>  iptr = jptr;
>}
>
>	I know I could have passed back the pointer in a return statement,
>I wanted to get this done correctly for now.  In the main program,
>ip is <nil> on return, instead of pointing to jptr as I had hoped.
>
>	Can someone explain what I should have done to get the desired
>effect?  Thanks in advance for replies and suggestions.
>
>regards,
>
>	ric steinberger
>	ric at ace.sri.com

Hi, ric steinberger. if you knew "all parameters passed by value" in c,then
you could easily figure out where your code went wrong. your problem is a
typical c programming problem. I would suggest you to pass the address of 'ip'
and the rest of your code should be changed accordingly.

good luck, ric.

xwang

xwang at gmuvax2.gmu.edu



More information about the Comp.lang.c mailing list