Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: usecases of void foo(char **b) {} Date: Sun, 30 Oct 2016 12:52:24 -0700 Organization: None to speak of Lines: 50 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="8244d122f3e691a199aa31cb28bb3690"; logging-data="9290"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zixsN7uL4MM0uqOApS8Bp" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:UwxqDjO45ICNn/uSGhcu1+Yslg0= sha1:FzLR8MoXEryYYpni17JibZ3amwY= Xref: csiph.com comp.lang.c:95417 supercat@casperkitty.com writes: > On many implementations, all pointer types have the same size and > representation; in the days before void* was recognized as a valid > pointer type, function which accepted a char** could use it to manipulate > any kind of object pointer or an array thereof. There is of course no such guarantee, and in my opinion it is poor practice to depend on it. > wanted to find a function which would report whether an array of > pointers contains any nulls: > > int containsAnyNulls(char **p, int size) > { > if (size) do > { > if (!*p) return 1; > } while(--size); > return 0; > } I wonder why you wrote that as a do-while loop inside an if rather than more simply as a while loop. I initially thought the "if (size) do" was at typo. Is that intentionally obfuscated? > A caller would need to cast a pointer to the array to type (char**), and > on implementations where different pointer types had different > representations such code couldn't be expected to work at all, but in > the days prior to aliasing rules such a function could be used with > arrays of pointers to any kind of object. Once void* entered the > language, the type void** would generally have been preferred to char**, > but either type would have worked equally well on any implementation > where all pointers had the same size and representation [i.e. the vast > majority of implementations]. void* is a "generic" pointer type. void** is not a generic pointer-to-pointer type, and IMHO should not be used as one. If I needed to store arbitrary pointer-to-pointer values of different types, I'd use void* and some careful conversions. The OP was asking about use cases for a function that takes a char** argument. I hardly think that your digression about pointer sizes and aliasing rules is relevant to that question. If you wanted to show an example, you could have just discussed the argv parameter to main. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"