Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!rutgers!pyrnj!pyrdc!uunet!mcvax!unido!nixpbe!ugun21!josef From: josef@ugun21.UUCP Newsgroups: comp.sys.ibm.pc Subject: Re: char ***pointer; Message-ID: <18200002@ugun21> Date: 17 Nov 88 07:46:00 GMT References: <360@mjbtn.MFEE.TN.US> Lines: 67 Nf-ID: #R:mjbtn.MFEE.TN.US:-36000:ugun21:18200002:000:2538 Nf-From: ugun21.UUCP!josef Nov 17 08:46:00 1988 In his note, Mark J. Bailey (mjbtn.UUCP) writes: >Now I know that *ch is generally pointing to the start of a string, ie, >a list (array) of characters. **ch would be equal to say a list of lists >(or an array of strings, ie, **argv, for example). ***ch would support >a list of lists of lists of characters. I suppose this could be associated >with a two dimensional array of strings. > >My biggest question here is, when I declare a *ch and then call malloc >to allocate a space for it to point to, I have a string. I can ch++ to >move up the string. But when I have **ch, and subsequently, ***ch, do >I have to call malloc to allocate space for more pointers? The best thing to do (in my opinion) is to draw a little picture: Start out with the item You want to reference in the end (e.g. "item"), then add an indirection level for each asterisk, e.g.: item ***ip: +---+- -+- -+ +---+- -+- -+ +---+- -+- -+ +---+- -+- -+ item | | | |...| | | | | | | |...| | | | +---+- -+- -+ +---+- -+- -+ +---+- -+- -+ +---+- -+- -+ ^ ^ ^ ^ | +--- +-------+ +-----------+ | | | | +-+-+- -+- -+-+-+ +-+-+- -+- -+-+-+ *item | o | | | o | ... | o | | | o | +---+- -+- -+- -+ +---+- -+- -+- -+ ^ ^ | +-----------+ | | +-+-+- -+- -+-+-+ **item | o | | | o | +-+-+- -+- -+- -+ ^ | +-+-+ ***item | o | +---+ With Your declaration "item ***ip;" all You get is Your ***item pointer, containing garbage, i.e. pointing to nothing in particular. This means You'll have to do 1) at least one malloc/calloc/... for the **item (-array) of size, say x ---> ip = (item ***) malloc(x * sizeof(item **)) 2) x mallocs/callocs/... for the *item (-array)s of variable sizes yi ---> ip[i] = (item **) malloc(yi * sizeof(item *)) 3) x*(y0+y1+y2+...+yn) mallocs/callocs/... for the item (-array)s of sizes zj. ---> ip[i][j] = (item *) malloc(zj * sizeof(item)) Of course, You can choose different numbers of elements in each the 2nd and 3rd step (i.e. the yis do not all have to be the same or more formally the assertion yn==ym must not hold for n!=m, the same applies for the zjs.) Hope this shed a little light on the subject 8-) Josef Moellers paper mail: e-mail: c/o Nixdorf Computer AG USA: uunet!linus!nixbur!nixpbe!mollers.pad Abt. EG-3 !USA: mcvax!unido!nixpbe!mollers.pad Unterer Frankfurter Weg D-4790 Paderborn tel.: (+49) 5251 104691 Standard disclaimer: Blablabla opinion blablabla employer blablabla!