Q: Realloc of function parameter problem.

Satish Kumar .C chittamu at umvlsi.ecs.umass.edu
Mon Nov 27 16:32:21 AEST 1989


In article <3752 at umiami.miami.edu> SLORES at umiami.miami.edu (Stanislaw L. Olejniczak) writes:
>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.
>

What compiler are you using? I tried it under gcc and the Ultrix cc. It
worked fine. Ofcourse I made one small change. Your last parameter to the
function substr was also named substr. I changed the parameter to a
different name. Could it have something to do with that?

===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 **subs)
{
   /* The following statement compiled fine under gcc and pcc */

    *subs = ( (char *) realloc ( *subs,(unsigned) (numchar + 1)));
}


=========
-- 
	-Satish.
	chittamu at cs.umass.edu
--
The Theory of Objectivity: E = MC++



More information about the Comp.lang.c mailing list