Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Why C has Pointers. |
| Date | 2011-12-17 20:36 -0800 |
| Organization | http://groups.google.com |
| Message-ID | <19358625.330.1324182992209.JavaMail.geo-discussion-forums@yqey7> (permalink) |
| References | (2 earlier) <golpe7lj73rnopq19ndti2hq1k02v02cat@4ax.com> <vqrpe7pkb15dek159fddlo2kfepn9dtcvl@4ax.com> <lnpqfm50db.fsf@nuthaus.mib.org> <0nbqe7p03fis0cbr7l4q0v3nbkli0ql6am@4ax.com> <0.cf93faba882b86d069dd.20111218012659GMT.87iplellek.fsf@bsb.me.uk> |
On Sunday, December 18, 2011 9:26:59 AM UTC+8, Ben Bacarisse wrote: > ralph <nt_cons...@yahoo.net> writes: > > > On Sat, 17 Dec 2011 13:55:44 -0800, Keith Thompson <ks...@mib.org> > > wrote: > > > >>ralph <nt_cons...@yahoo.net> writes: > >>[...] > >>> I have trouble understanding the constant confusion between arrays and > >>> pointers. Way back when (and we are talking many moons ago <g>) when I > >>> started out in C my mentor explained it very simply in these terms in > >>> the first couple of days. > >>> > >>> 1) There is a difference between declarations and definitions. > >>> Remember which you are dealing with and when. > >>> 2) A pointer is a "variable", an array is a "constant" > >> > >>That doesn't really make sense. An array variable is not a constant: > >> > >> int arr[10]; > >> arr[5] = 42; > >> > >>I suppose what it means is that an array name, when it appears in an > >>expression (in most but not all context) decays to a pointer to the > >>array's first element, and that pointer value cannot be modified. But I > >>think that saying an array is a constant just reinforces the confusion > >>between arrays and pointers. > >> > > > > I disagree. > > > > Once the beginner gets it through his head that you can never do an > > assignment to 'arr' you resolve a ton of the questions we get around > > here. That as close to the definition of a "constant" as you can get. > > I'd say that "unchanging" or "immutable" were closer definitions, and > arrays can be changed. > > What you call a constant, I'd call a value, and I'd explain that > array-valued expressions are (usually) converted automatically to > pointer values. > > Most people learning programming quickly get the idea of variables and > values. Variables can be assigned to, but values can't. This avoids > calling arrays "constant". It's also a start towards some more > technical matters such as why > > int x; > x = 0; > > is ok, but > > (int)x = 0; > > is not. > > >>> 3) The array name evaluates to the address of the first element in the > >>> array. > >> > >>This applies to any expression of array type, not just the name of a > >>declared array object. But it doesn't apply in all contexts. > >>Specifically, an expression of array type is converted to a pointer to > >>the array object's first element *unless* it's: > >> > >> The operand of a unary "sizeof" operator (sizeof arr gives you the > >> same of the array, not of a pointer); > >> > >> The operand of a unary "&" operator (&arr gives you the address of > >> the array object; it's the same memory location as the address of > >> its first element, but it's of a different type); > >> > >> A string literal in an initializer used to initialize an array > >> object. In > >> char arr[6] = "hello"; > >> the *value* of "hello" is copied into arr. > >> > > > > Yep. But what's the big deal. > > > > And of course I disagree that it actually matters to the beginner that > > the address is of a "different type". Semantically and syntactically > > it comes out the same. Plenty of time to get into vague language > > later. > > I agree about putting this off, but 'arr' and '&arr' are not the same > semantically. If they were, > > arr + 1 > > and > > &arr + 1 > > would have the same value but they don't. (I'm using arr as defined > above.) > > <snip> > >>In most contexts; see above. It would be clearer to say that an array > >>name *decays to* an address (or pointer value). And I wouldn't use the > >>word "initialized". > > > > I would. And again what does 'decay' meant to the beginner? > > In code the name "arr" is changed to, or evaluates to, the address of > > the first element. > > I'd just say "converted". C has lots of automatic conversions and, > sooner or later, these need to be explains. The array to pointer > conversion fits into this general pattern. > > <snip> > >>> 9) The array operator evaluates to a storage map equation, eg. > >>> type ar[i] := *(a + (i * sizeof(type)) > >> > >>I'm not sure what syntax you're using. What does "type" mean here? And > >>C pointer arithmetic is already scaled by the size of the pointed-to > >>type. > >> > >>arr[i] is by definition equivalent to *(arr + i). > > > > Think about it. > > > > You say that now, but you come back later in other response to point > > out "sizes". That "already scaled" is because the compiler knows the > > type, and he knows the type because you declared it. > > It's not easy to explain the lower-level arithmetic without reference to > some particular machine. For example, on a machine with, say, byte and > word addresses, you can't describe it as you have done. By all means, > take a simple architecture and explain what a[0] means on one particular > system using addresses and size calculations, but you will also have to > explain, eventually, that arr[i] is, by definition, *(arr + i). > > <snip> > -- > Ben. In C there is no boundary checking in the index of an array in a read or write operation to the array.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why C has Pointers. curixinfotech <curixinfotech.54dl3z@no-mx.forums.yourdomain.com.au> - 2011-12-07 07:22 -0500
Re: Why C has Pointers. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-12-07 04:35 -0800
Re: Why C has Pointers. Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-07 07:58 -0500
Re: Why C has Pointers. Mike Manilone <zhtx10@gmail.com> - 2011-12-07 13:15 +0000
Re: Why C has Pointers. Bill Reid <hormelfree@gmail.com> - 2011-12-08 06:29 -0800
Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@hotmail.it> - 2011-12-07 14:50 +0100
Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-07 11:01 -0800
Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-12-07 20:49 +0100
Re: Why C has Pointers. Brian <nospam@nospam.com> - 2011-12-07 20:00 +0000
Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-07 15:30 -0500
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-07 12:43 -0800
Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-07 13:09 -0800
Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@hotmail.it> - 2011-12-08 19:39 +0100
Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-08 14:16 -0500
Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@hotmail.it> - 2011-12-08 20:23 +0100
Re: Why C has Pointers. Kaz Kylheku <kaz@kylheku.com> - 2011-12-07 20:48 +0000
Re: Why C has Pointers. Vincenzo Mercuri <vincenzo.mercuri@yahoo.it> - 2011-12-07 22:07 +0100
Re: Why C has Pointers. Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-10 07:34 +0000
Re: Why C has Pointers. August Karlstrom <fusionfile@gmail.com> - 2011-12-07 16:16 +0100
Re: Why C has Pointers. Borax Man <rotflol2@hotmail.com> - 2011-12-08 18:32 +1100
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-08 00:20 -0800
Re: Why C has Pointers. André Gillibert <MetaEntropy.removeThis@gmail.com> - 2011-12-08 13:44 +0100
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-09 05:04 -0800
Re: Why C has Pointers. gazelle@shell.xmission.com (Kenny McCormack) - 2011-12-09 14:59 +0000
Re: Why C has Pointers. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-12-09 20:19 -0800
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-10 07:36 -0800
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-08 00:12 -0800
Re: Why C has Pointers. Cong Wang <xiyou.wangcong@gmail.com> - 2011-12-15 07:26 +0000
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 08:51 -0800
Re: Why C has Pointers. Barry Schwarz <schwarzb@dqel.com> - 2011-12-17 10:10 -0800
Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 14:28 -0600
Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 15:00 -0600
Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-17 13:55 -0800
Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 18:27 -0600
Re: Why C has Pointers. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-18 01:26 +0000
Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 22:34 -0600
Re: Why C has Pointers. "Philip Lantz" <prl@canterey.us> - 2011-12-17 23:48 -0800
Re: Why C has Pointers. superpollo <superpollo@tznvy.pbz> - 2011-12-18 11:06 +0100
Re: Why C has Pointers. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-18 13:44 +0000
Re: Why C has Pointers. pete <pfiland@mindspring.com> - 2011-12-18 11:05 -0500
Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-18 11:50 -0500
Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-18 13:10 -0800
Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-27 16:03 -0600
Re: Why C has Pointers. Barry Schwarz <schwarzb@dqel.com> - 2011-12-27 14:40 -0800
Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-27 15:08 -0800
Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-27 20:09 -0600
Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-27 23:00 -0800
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 02:53 -0800
Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-28 08:39 -0500
Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-27 18:14 -0500
Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-27 15:36 -0800
Re: Why C has Pointers. James Kuyper <jameskuyper@verizon.net> - 2011-12-27 18:53 -0500
Re: Why C has Pointers. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-28 13:20 +0000
Re: Why C has Pointers. John Bode <jfbode1029@gmail.com> - 2011-12-28 08:42 -0800
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 20:36 -0800
Re: Why C has Pointers. Seebs <usenet-nospam@seebs.net> - 2011-12-18 04:56 +0000
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 21:37 -0800
Re: Why C has Pointers. Seebs <usenet-nospam@seebs.net> - 2011-12-18 05:42 +0000
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-18 08:08 -0800
Re: Why C has Pointers. Robert Wessel <robertwessel2@yahoo.com> - 2011-12-18 10:36 -0600
Re: Why C has Pointers. Seebs <usenet-nospam@seebs.net> - 2011-12-18 18:50 +0000
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-19 02:39 -0800
Re: Why C has Pointers. gordonb.1i27q@burditt.org (Gordon Burditt) - 2011-12-21 22:34 -0600
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-19 07:56 -0800
Re: Why C has Pointers. John Tsiombikas <nuclear@member.fsf.org> - 2011-12-19 18:38 +0000
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-19 13:47 -0800
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 02:58 -0800
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-28 03:21 -0800
Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-18 13:07 -0800
Re: Why C has Pointers. "io_x" <a@b.c.invalid> - 2011-12-19 04:02 +0100
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 15:35 -0800
Re: Why C has Pointers. ralph <nt_consulting64@yahoo.net> - 2011-12-17 18:41 -0600
Re: Why C has Pointers. John Bode <jfbode1029@gmail.com> - 2011-12-19 05:14 -0800
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-18 09:50 -0800
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-09 15:48 -0800
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-10 00:21 -0800
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-10 01:03 -0800
Re: Why C has Pointers. Keith Thompson <kst-u@mib.org> - 2011-12-17 11:45 -0800
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-17 15:18 -0800
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-19 14:10 -0800
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-20 10:46 -0800
Re: Why C has Pointers. "Skybuck Flying" <Windows7IsOK@DreamPC2006.com> - 2011-12-17 19:15 +0100
Re: Why C has Pointers. "Skybuck Flying" <Windows7IsOK@DreamPC2006.com> - 2011-12-17 19:19 +0100
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-19 02:48 -0800
Re: Why C has Pointers. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-18 11:05 -0800
Re: Why C has Pointers. Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-19 02:48 -0800
csiph-web