Q: Realloc of function parameter problem.

brian_helterline brianh at hpcvia.CV.HP.COM
Tue Nov 28 06:42:27 AEST 1989


>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 at umiami.miami.edu
>University of Miami, FL USA   UUCP: (temp void) gould!umbio!solejni
>SLORES at UMIAMI.BITNET          UUCP: (?)         umigw!gables!slores
>Voice: (305) 547-6571         FAX: (305) 548-4612  
>My opinions cannot possibly represent the views of anyone else!
>----------



More information about the Comp.lang.c mailing list