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: Sat, 20 Aug 2016 16:00:56 -0700 Organization: None to speak of Lines: 56 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="16461"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/O7ijYmfQr1h6m6PXJy4PV" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:lZ8EYifhXbSQ0BTUlQflqpDwvEE= sha1:EIfvUhm15WEhpt/h0Mg8eAsY4/c= Xref: csiph.com comp.lang.c:92553 BartC writes: [...] > You mentioned structs. Perhaps they might serve as an easier comparison > with arrays as there are several points of similarity: > > - Arrays have N elements of the same type; so can a struct /when all > members share the same type/ > > - Declaring a top-level array allocates space for N elements in both > cases > > - 'sizeof' applied to an array or struct type returns the total bytes > it occupies > > - & applied to an array or struct returns a pointer of a type that > encompasses the entire array or struct > > Now we come to the differences: > > - An array name in an expression (outside the exceptions) yields a > pointer to the first element: A becomes &A[0]. A struct S yields > the entire struct A small quibble: the same rule applies to any expression of array type, not just an array name. > - An array as a function parameter is interpreted as a pointer to > its element type. A struct as a function parameter remains a struct > > - To allocate a struct of type S, the result of malloc() is assigned to > a pointer-to-S, or a S* type; to allocate an array A of type T, the > common idiom is to assign to a pointer-to-T type, not a pointer-to-A > type. > > (The latter is also possible, but apparently causes confusion as it > is assumed to be of the pointer-to-T pattern, so with pointer-to-A, > the language, and many C experts, expect that to be part of a larger > array with extra dimension. Ie. a dynamic array of A, or an array of > array of T. This is unfortunate.) > > Interestingly, a bounded array can be wrapped up in a struct so that its > behaviour changes, but it becomes more fiddly to access the elements and > is generally more hassle. Even more interestingly, that's a reasonably good summary of how arrays and pointers actually behave in C. So why the *hell* have you been pretending that you're so confused by it? It seems that you've been deliberately trolling us, and I've been trying to help someone who was only pretending to need it. -- 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"