home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19599 < prev    next >
Encoding:
Text File  |  1993-01-12  |  1.8 KB  |  62 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!mcsun!sun4nl!dutrun!donau!dutecaj.et.tudelft.nl!johnvl
  3. From: johnvl@dutecaj.et.tudelft.nl (John C.M. van Leeuwen)
  4. Subject: Re: realloc (again)
  5. Message-ID: <1993Jan12.150745.13027@donau.et.tudelft.nl>
  6. Keywords: Realloc
  7. Sender: news@donau.et.tudelft.nl (UseNet News System)
  8. Nntp-Posting-Host: dutecaj.et.tudelft.nl
  9. Organization: Delft University of Technology, Dept. of Electrical Engineering
  10. References:  <C0pGL7.GqI@compsci.liverpool.ac.uk>
  11. Date: Tue, 12 Jan 1993 15:07:45 GMT
  12. Lines: 48
  13.  
  14. In article <C0pGL7.GqI@compsci.liverpool.ac.uk>, scst81@csc.liv.ac.uk (Mr. I. Rowland) writes:
  15. > Following my last communication I developed another smaller program that
  16. >   uses REALLOC in the same way as my other code:
  17. >   #include <stdio.h>
  18. >   #include <stdlib.h>
  19. >   #include <string.h>
  20. >   #define noofstr 5
  21. >   #define bufsiz 255
  22. >   void main (void)
  23. >   {
  24. >           char *array; char buff[bufsize];
  25. >           int i;
  26. >           int bytes ;
  27. >           for (i=0; i < noofstr; i++)
  28. >           {
  29. >                   printf("Enter string %d : ",i);
  30. >                   scanf("%255s",buff);
  31. >                   bytes = strlen(buff)+1;
  32. >                   array = reilloc(array, (bytes));
  33. >                   if (array== NULL)
  34. >                   {
  35. >                           printf("Can't allocate");
  36. >                           exit(1);
  37. >                   }
  38. >           strcat (array,buff);
  39. >           }
  40. >                   printf(array);
  41. >   }
  42.  
  43. In the manual I read: the pointer passed to realloc()  (here: array)
  44. has to be initialised before using one of the normal allocation functions.
  45. So try:
  46.            char *array = "";
  47.  
  48. at the beginning instead. I guess you'll get rid of those ugly messgaes this way
  49. .
  50.  
  51. John van Leeuwen.  (j.c.m.vanleeuwen@et.tudelft.nl)
  52.