home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!portal!dfuller
- From: dfuller@portal.hq.videocart.com (Dave Fuller)
- Subject: Re: How to malloc an array of pointers?
- Message-ID: <Bxyv6B.5FL@portal.hq.videocart.com>
- Organization: VideOcart Inc.
- X-Newsreader: Tin 1.1 PL3
- References: <1992Nov19.004950.9200@icaen.uiowa.edu>
- Distribution: usa
- Date: Thu, 19 Nov 1992 14:06:10 GMT
- Lines: 34
-
- sbtaves@icaen.uiowa.edu (Steven Bradley Taves) writes:
- : Hi all,
- :
- : I'd appreciate it if someone could tell me how I can allocate memory
- : for an array of pointers and for the strings they will be pointing to.
- :
- : I have
- : char *Var[];
- :
- : Normally for
- : char *Var;
- : you do
- : Var=(char *)malloc(10*sizeof(char));
- : for a string of 10 characters.
- :
- : I want to allocate memory for 10 strings of ten characters. Also, how
- : do I allocate memory for the pointers themselves?
- :
-
- try this:
-
- instead of char *Var[] -> char *Var[10] (since you know you have 10)
- this will also allocate the memory for you automatically (for pointers)
-
- then without any error checking of course
-
- for (i = 0; i < 10; i++)
- Var[i] = (char *) malloc(10 * sizeof(char));
-
- thats all.
-
- Dave Fuller
- dfuller@portal.hq.videocart.com
-
-