Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!mcsun!ukc!edcastle!aiai!richard From: richard@aiai.ed.ac.uk (Richard Tobin) Newsgroups: comp.lang.c Subject: Re: SIMPLE malloc & pointer question Message-ID: <3143@skye.ed.ac.uk> Date: 7 Aug 90 14:22:03 GMT References: <7206@helios.TAMU.EDU> Reply-To: richard@aiai.UUCP (Richard Tobin) Organization: AIAI, University of Edinburgh, Scotland Lines: 35 In article <7206@helios.TAMU.EDU> jdm5548@diamond.tamu.edu (James Darrell McCauley) writes: > inita(a,b); > printf("main(): a[2]=%d\n",a[2]); >inita (a,b) >int a[],b[]; >{ > a=(int *)malloc( (unsigned) 4*sizeof(int)); > a[2]=3; Arguments in C are passed by value, not reference. inita() gets a copy of main()'s variable "a", so when inita() returns the value in main() is unchanged. If you want to modify a variable in the parent procedure, you should pass a pointer to it: inita(&a,b); and declare the argument appropriately: int **a; /* a is a pointer to a pointer to integers */ and assign to what the argument points to, not the argument itself (*a)=(int *)malloc( (unsigned) 4*sizeof(int)); (*a)[2]=3; -- Richard -- Richard Tobin, JANET: R.Tobin@uk.ac.ed AI Applications Institute, ARPA: R.Tobin%uk.ac.ed@nsfnet-relay.ac.uk Edinburgh University. UUCP: ...!ukc!ed.ac.uk!R.Tobin