home *** CD-ROM | disk | FTP | other *** search
- Nntp-Posting-Host: solva.ifi.uio.no
- Newsgroups: comp.lang.c
- Path: sparky!uunet!mcsun!sunic!aun.uninett.no!nuug!ifi.uio.no!nntp.ifi.uio.no!jar
- From: jar@solva.ifi.uio.no (Jo Are Rosland)
- Subject: Re: array of file pointer ?
- In-Reply-To: kjlee@mtu.edu's message of Wed, 16 Dec 1992 02:17:38 GMT
- Message-ID: <JAR.92Dec16154044@solva.ifi.uio.no>
- X-Md4-Signature: 4a3754deb772f4387c2b9d32dd71ceed
- Sender: jar@ifi.uio.no (Jo Are Rosland)
- Organization: Dept. of Informatics, University of Oslo, Norway
- References: <1992Dec16.021738.2599@mtu.edu>
- Date: Wed, 16 Dec 1992 14:40:44 GMT
- Lines: 37
- Originator: jar@solva.ifi.uio.no
-
- In article <1992Dec16.021738.2599@mtu.edu> kjlee@mtu.edu (KONG-JIN LEE) writes:
-
- Does anyone know how to declare an array of file pointer in GCC
-
- I have tried :
-
- FILE *filptr[10];
-
- ???? ---> is this ok ? FILE **filptr;
- if (0k)
-
- then ??? ---> filptr = malloc(10*sizeof(FILE));
- ??? ---> *(filptr+10) = fopen(......);
-
- Close, but not quite. You need to do it like this:
-
- filptr = (FILE **)malloc(10 * sizeof(FILE *))
- *(filptr + 9) = fopen(......);
-
- This has three differences from your original suggestion:
-
- 1. The return value from malloc is cast'ed to the type it's
- supposed to have.
-
- 2. You want to make an array of FILE * instead of FILE, hence
- use FILE * inside sizeof.
-
- 3. filptr + 10 points beyond the end of the memory allocated by
- malloc, while filptr + 9 points to the last element.
-
- And last, instead of the second line you can equally well write:
-
- filptr[9] = fopen(......);
-
- --
- Jo Are Rosland
- jar@ifi.uio.no
-