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: Difference between these pointer syntaxes Date: Wed, 17 Aug 2016 17:20:41 -0700 Organization: None to speak of Lines: 108 Message-ID: References: <4628cd2d-9333-4be6-943a-eb593c5c4c12@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="10893"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Fbki84h7PAmKZJnZGIHCi" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:R0kZAYCsfiP4QTiWHSSlhUOTm10= sha1:P9w8z5Hhq6KKzMmJHiJ+uYpXx7g= Xref: csiph.com comp.lang.c:92418 Les Cargill writes: > Keith Thompson wrote: >> Les Cargill writes: >>> already5chosen@yahoo.com wrote: >>>> On Tuesday, August 16, 2016 at 5:20:52 PM UTC+3, Keith Thompson wrote: >>>>> Your claim that arrays and pointers are the same thing is blatantly >>>>> incorrect. >>>> >>>> The sentence "C arrays and pointers are the same thing" is incorrect >>>> in fine details. The sentence "C arrays and pointers have nothing in >>>> common" is blatantly incorrect. >>> >>> I believe that "C arrays are the same as 'C' pointers except for >>> certain constraints" is both correct and complete. >> >> No. >> >>> Array semantics are a proper subset of pointer semantics. >> >> No. >> >> Are you talking about array and pointer types? Array and pointer >> objects? Array and pointer expressions? Array and pointer >> declarations? Array and pointer values? Do you claim that array >> semantics are a proper subset of pointer semantics for all those >> things? >> >> If array semantics were a proper subset of pointer semantics, we >> wouldn't need arrays; we could do everything with just pointers. > > Well, we *can*. There's never a reason to ever use an array in 'C' > except to allocate a quantity of memory statically as a declaration. > > That could be done using another mechanism. What mechanism? Please demonstrate. > it turns out that array notation seems better for some things; it > can be cleaner. > >> Please demonstrate how to write a reasonable "hello, world" >> program, or how to process command-line arguments (argc and argv) >> without using any arrays, or how to use any of the standard library >> functions that take string pointers as arguments. Please demonstrate your non-array "hello, world" program. > argv can be declared **argv. It isn't, by convention, but there's > nothing untoward about it. "*argv[]" is a little cleaner than "**argv". There is no difference between `char **argv` and `char *argv[]` as parameter declarations. The standard uses `char *argv[]`; I personally prefer `char **argv` because it reflects the semantics more closely. Please show me a C program that processes command-line arguments without referring to any array object. >> Note that in >> printf("hello, world\n"); >> the string literal is an array expression. The word "reasonable" >> is intended to exclude solutions that process each character >> individually. >> > > Which is fine. > >> In >> int main(int argc, char **argv) >> argv is a pointer, but it points to the initial element of an array >> object, and each non-null element of that array object also points >> to the initial element of an array object. (All those array objects >> are allocated somehow before main is entered). > > but a "pointer to a pointer" and an "array of pointers" differ only > in that one may use sizeof() to determine the extent of an array of > pointers if they array of pointers is in scope. No, in this case they differ in that there are *array objects* that are allocated before `main` is entered. >> (It's 2016. I can't believe I still have to argue that arrays are >> not pointers.) > > because it's a perfectly workable abstraction. No, it isn't. It's an abstraction that leaves many things unexplained. *Maybe* you could create a set of special-case rules that could give a consistent model, but you'd end up with something much more complicated that the way the language is described in the standard. It might be possible to construct a consistent description of C that asserts that "arrays are really pointers", but I've never seen such a description. Can you explain the behavior of `sizeof` while pretending that there's no such thing as an array? (Array type, array object, array whatever.) Can you explain what int arr[10]; does in terms of pointers, without references to arrays? Can you explain the result of `sizeof arr`? -- 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"