www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/11/14:59:18

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: array of pointers to strings, BC++, DJGPP and weird printing , behavior
Date: Mon, 11 Nov 1996 08:15:58 -0800
Organization: Alcyone Systems
Lines: 38
Message-ID: <3287513E.100CF1E0@alcyone.com>
References: <96Nov11 DOT 100152gmt DOT 26498 AT spy DOT viglen DOT co DOT uk>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Toby Ounsted wrote:


> > > : >         KeyWords = (char **) malloc ( sizeof(char) );

> Okay...I *MUST* be a genius then, because I already knew that...let me
> clarify..I've seen, used and even explained type casting in numerous
> situations, I've also seen, used and even explaine dthe purpose and
> methods behind allocating memory for arrays of..well...anything I need
> arrays of...what I don't recognize is that double asterisk..*THAT* I've
> never seen...which is what I was referring to..so, what did I miss, if
> anything, and when?  DJGPP doesn't lend much to online syntax referral,
> in any way(that I know).

This is ANSI C; nothing DJGPP-specific.  You should get a book on ANSI C.

To allocate an array of N ints using malloc, one uses the following syntax:

    int *array = (int *) malloc(N*sizeof(int));

Note how one needs a pointer to an int to store the dynamically-allocated
array.  malloc's void * return value must be cast to the appropriate type
to appease the ANSI C gods.

Now if, on the other hand, one wants an array of N _pointers to ints_, then
one uses the same rule to come up with the syntax:

    int **array = (int **) malloc(N*sizeof(int *));

Since what you want is an array of pointers, you need a pointer to a
pointer to point to it.

-- 
                             Erik Max Francis | max AT alcyone DOT com
                              Alcyone Systems | http://www.alcyone.com/max/
                         San Jose, California | 37 20 07 N 121 53 38 W
                                 &tSftDotIotE | R^4: the 4th R is respect
         "But since when can wounded eyes see | If we weren't who we were"

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019