home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!mcsun!sun4nl!dutrun!donau!dutecaj.et.tudelft.nl!johnvl
- From: johnvl@dutecaj.et.tudelft.nl (John C.M. van Leeuwen)
- Subject: Re: realloc (again)
- Message-ID: <1993Jan12.150745.13027@donau.et.tudelft.nl>
- Keywords: Realloc
- Sender: news@donau.et.tudelft.nl (UseNet News System)
- Nntp-Posting-Host: dutecaj.et.tudelft.nl
- Organization: Delft University of Technology, Dept. of Electrical Engineering
- References: <C0pGL7.GqI@compsci.liverpool.ac.uk>
- Date: Tue, 12 Jan 1993 15:07:45 GMT
- Lines: 48
-
- In article <C0pGL7.GqI@compsci.liverpool.ac.uk>, scst81@csc.liv.ac.uk (Mr. I. Rowland) writes:
- > Following my last communication I developed another smaller program that
- > uses REALLOC in the same way as my other code:
- >
- > #include <stdio.h>
- > #include <stdlib.h>
- > #include <string.h>
- >
- > #define noofstr 5
- >
- > #define bufsiz 255
- >
- > void main (void)
- >
- > {
- > char *array; char buff[bufsize];
- > int i;
- > int bytes ;
- >
- > for (i=0; i < noofstr; i++)
- > {
- > printf("Enter string %d : ",i);
- > scanf("%255s",buff);
- > bytes = strlen(buff)+1;
- > array = reilloc(array, (bytes));
- >
- > if (array== NULL)
- > {
- > printf("Can't allocate");
- > exit(1);
- > }
- > strcat (array,buff);
- > }
- >
- >
- >
- > printf(array);
- > }
-
- In the manual I read: the pointer passed to realloc() (here: array)
- has to be initialised before using one of the normal allocation functions.
- So try:
- char *array = "";
-
- at the beginning instead. I guess you'll get rid of those ugly messgaes this way
- .
-
- John van Leeuwen. (j.c.m.vanleeuwen@et.tudelft.nl)
-