Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!hellgate.utah.edu!helios.ee.lbl.gov!pasteur!ucbvax!hplabs!hp-pcd!hpcvia!brianh From: brianh@hpcvia.CV.HP.COM (brian_helterline) Newsgroups: comp.lang.c Subject: Re: Q: Realloc of function parameter problem. Message-ID: <18060003@hpcvia.CV.HP.COM> Date: 27 Nov 89 19:42:27 GMT References: <3752@umiami.miami.edu> Organization: Hewlett-Packard Co., Corvallis, Oregon Lines: 43 >I am trying to write a function which would, if neccessary, change the >size of the string pointed to by one of the arguments, and return the >pointer to the "new" string. It is part of a larger function. I seem >to have a problem with realloc... I tried finding an answer in just about >all the C books I have, but I simply cannot figure out WHY is it giving me >problems. If you could tell me HOW to do this correctly, I would be most >grateful. Any assistance would be welcome. >code: >====================================================================== >char *substr( char *, int, int, char **); >main() >{ > char *string="This is a test", > *subs=""; > int start = 3, > numchar = 5; > > subs = substr (string, start, numchar, &subs); >} > >char *substr (char *string, int start, int numchar, char **substr) >{ > /*I get a variety of errors on the following statement, depending on a > compiler/linker. The code bombs here, I guess because memory > management is being damaged*/ > > *substr = (char *) realloc ( (unsigned) (numchar + 1), *substr); ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ I believe you need to switch these. At least for MSC 5.1, you specify the buffer first, followed by the size. >} > > >---- >Stan Olejniczak Internet: slores@umiami.miami.edu >University of Miami, FL USA UUCP: (temp void) gould!umbio!solejni >SLORES@UMIAMI.BITNET UUCP: (?) umigw!gables!slores >Voice: (305) 547-6571 FAX: (305) 548-4612 >My opinions cannot possibly represent the views of anyone else! >----------