home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16793 < prev    next >
Encoding:
Text File  |  1992-11-19  |  1.2 KB  |  47 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!portal!dfuller
  3. From: dfuller@portal.hq.videocart.com (Dave Fuller)
  4. Subject: Re: How to malloc an array of pointers?
  5. Message-ID: <Bxyv6B.5FL@portal.hq.videocart.com>
  6. Organization: VideOcart Inc.
  7. X-Newsreader: Tin 1.1 PL3
  8. References: <1992Nov19.004950.9200@icaen.uiowa.edu>
  9. Distribution: usa
  10. Date: Thu, 19 Nov 1992 14:06:10 GMT
  11. Lines: 34
  12.  
  13. sbtaves@icaen.uiowa.edu (Steven Bradley Taves) writes:
  14. : Hi all,
  15. :     I'd appreciate it if someone could tell me how I can allocate memory
  16. : for an array of pointers and for the strings they will be pointing to.
  17. : I have  
  18. :        char *Var[];
  19. : Normally for
  20. :        char *Var;
  21. : you do
  22. :        Var=(char *)malloc(10*sizeof(char));
  23. : for a string of 10 characters.
  24. : I want to allocate memory for 10 strings of ten characters.  Also, how
  25. : do I allocate memory for the pointers themselves?
  26.  
  27. try this:
  28.  
  29. instead of char *Var[] -> char *Var[10]  (since you know you have 10)
  30. this will also allocate the memory for you automatically (for pointers)
  31.  
  32. then without any error checking of course
  33.  
  34. for (i = 0; i < 10; i++)
  35.    Var[i] = (char *) malloc(10 * sizeof(char));
  36.  
  37. thats all.
  38.  
  39. Dave Fuller
  40. dfuller@portal.hq.videocart.com
  41.  
  42.