Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!sample.eng.ohio-state.edu!purdue!haven.umd.edu!umbc3.umbc.edu!gmuvax2!xwang From: xwang@gmuvax2.gmu.edu (Xiang-Min Wang) Newsgroups: comp.lang.c Subject: Re: Simple ptr passing question Message-ID: <1991May22.204153.3967@gmuvax2.gmu.edu> Date: 22 May 91 20:41:53 GMT References: <24268@unix.SRI.COM> Organization: George Mason University, Fairfax, Va. Lines: 49 In article <24268@unix.SRI.COM> ric@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 >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 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@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@gmuvax2.gmu.edu