home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18464 < prev    next >
Encoding:
Text File  |  1992-12-16  |  1.6 KB  |  53 lines

  1. Nntp-Posting-Host: solva.ifi.uio.no
  2. Newsgroups: comp.lang.c
  3. Path: sparky!uunet!mcsun!sunic!aun.uninett.no!nuug!ifi.uio.no!nntp.ifi.uio.no!jar
  4. From: jar@solva.ifi.uio.no (Jo Are Rosland)
  5. Subject: Re: array of file pointer ?
  6. In-Reply-To: kjlee@mtu.edu's message of Wed, 16 Dec 1992 02:17:38 GMT
  7. Message-ID: <JAR.92Dec16154044@solva.ifi.uio.no>
  8. X-Md4-Signature: 4a3754deb772f4387c2b9d32dd71ceed
  9. Sender: jar@ifi.uio.no (Jo Are Rosland)
  10. Organization: Dept. of Informatics, University of Oslo, Norway
  11. References: <1992Dec16.021738.2599@mtu.edu>
  12. Date: Wed, 16 Dec 1992 14:40:44 GMT
  13. Lines: 37
  14. Originator: jar@solva.ifi.uio.no
  15.  
  16. In article <1992Dec16.021738.2599@mtu.edu> kjlee@mtu.edu (KONG-JIN  LEE) writes:
  17.  
  18.    Does anyone know how to declare an array of file pointer in GCC
  19.  
  20.    I have tried :
  21.  
  22.    FILE *filptr[10];
  23.  
  24.    ???? ---> is this ok ?  FILE **filptr;
  25.    if (0k)
  26.  
  27.    then ??? ---> filptr = malloc(10*sizeof(FILE));
  28.     ??? ---> *(filptr+10) = fopen(......); 
  29.  
  30. Close, but not quite.  You need to do it like this:
  31.  
  32.        filptr = (FILE **)malloc(10 * sizeof(FILE *))
  33.        *(filptr + 9) = fopen(......);
  34.  
  35. This has three differences from your original suggestion:
  36.  
  37. 1. The return value from malloc is cast'ed to the type it's
  38.    supposed to have.
  39.  
  40. 2. You want to make an array of FILE * instead of FILE, hence
  41.    use FILE * inside sizeof.
  42.  
  43. 3. filptr + 10 points beyond the end of the memory allocated by
  44.    malloc, while filptr + 9 points to the last element.
  45.  
  46. And last, instead of the second line you can equally well write:
  47.  
  48.        filptr[9] = fopen(......);
  49.  
  50. --
  51. Jo Are Rosland
  52. jar@ifi.uio.no
  53.