Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Issue on assigning address to pointers |
| Date | 2015-11-17 18:31 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <n2frlm$bbm$1@dont-email.me> (permalink) |
| References | <dc62f43a-1725-496b-b36a-c3e196015b7d@googlegroups.com> |
On 17/11/15 16:49, Alla _ wrote:
> Hello!
>
> I have seen many times the following declaration of pointers to arrays,
> advised without any caveats:
>
> int numbers[8];
> int *ptr;
>
> ptr = numbers + 8; // to point at a value stored at numbers[8]
No, it points at the location just past the array. (There isn't a
numbers[8] - the eight-element array's eight indices are 0, 1, 2, 3, 4,
5, 6, 7 - count them!)
Forming such a pointer is legal (which makes constructs such as:
for(ptrb = numbers; ptrb < ptr; ptr++)
{
dosomethingwith(*ptrb);
}
legal), but dereferencing ptr is not.
> Please, take a look at a paragraph from K&R, stating that this method
> doesn't work on "multi-tasking machines". Given K&R was published in
> 1999, I wonder if that statement still holds.
It isn't K&R. K&R wasn't published in 1999, but in 1978 (1st edition)
and 1988 (2nd edition). And it doesn't contain broken advice such as
that which you quote.
> QUOTE:
> char arrayname[5];
> char *ptr;
> for (ptr = arrayname; ptr <= arrayname+4; ptr++)
> {
> *ptr = 0;
> }
> The code above sets the array arrayname to zero.
No, it sets all elements of that array to 0, as does the far superior:
char arrayname[5] = {0};
> This method of getting
> at array data is not recommended by this author except in very simple
> computer environments. If a program is running on a normal microcomputer,
> then there should be few problems with this alternative method of handling
> arrays. On the hand, if the microcomputer is multi-tasking, or the program
> is running on a larger system which has a limited manager, then memory
> ceases to be something which can be thought of as a sequence of boxes
> standing next to one another. A multi-tasking system shares memory with
> Questions 175
> other programs and it takes what it can find, where it can find it. The upshot
> of this is that it is not possible to guarantee that arrays will be stored in one
> simple string of memory locations: it might be scattered around in different
> places.
At this point, you can safely discard the book. I suggest investing in
K&R instead. The 2nd edition is the one you want. I don't know what
you're smoking, but it isn't K&R.
> So
> ptr = arrayname + 5;
> might not be a pointer to the fifth character in a character array.
Indeed. It will be the sixth. Count them:
arrayname + 0 = pointer to the first element (element 0)
arrayname + 1 = pointer to the second element (element 1)
arrayname + 2 = pointer to the third element (element 2)
arrayname + 3 = pointer to the fourth element (element 3)
arrayname + 4 = pointer to the fifth element (element 4)
arrayname + 5 = pointer to the sixth element (element 5)
> This could
> be found instead using the '&' operator. A pointer to the fifth element can
> be reliably found with:
> ptr = &(arrayname[5]);
> Be warned!
Be warned that the book is teaching you nonsense.
arrayname[5] is exactly equivalent to *(arrayname + 5).
& and * cancel. So
ptr = &(arrayname[5]);
is exactly equivalent to:
ptr = arrayname + 5;
which is the very construct that the author (wrongly) warned you about.
He clearly doesn't know what he's doing, and needs to be ignored very
very loudly.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 08:49 -0800
Re: Issue on assigning address to pointers James Kuyper <jameskuyper@verizon.net> - 2015-11-17 12:36 -0500
Re: Issue on assigning address to pointers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-17 17:47 +0000
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 10:34 -0800
Re: Issue on assigning address to pointers Keith Thompson <kst-u@mib.org> - 2015-11-17 12:16 -0800
Re: Issue on assigning address to pointers James Kuyper <jameskuyper@verizon.net> - 2015-11-17 15:23 -0500
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 10:33 -0800
Re: Issue on assigning address to pointers Keith Thompson <kst-u@mib.org> - 2015-11-17 09:41 -0800
Re: Issue on assigning address to pointers Geoff <geoff@invalid.invalid> - 2015-11-17 10:03 -0800
Re: Issue on assigning address to pointers glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-17 20:31 +0000
Re: Issue on assigning address to pointers Geoff <geoff@invalid.invalid> - 2015-11-17 12:53 -0800
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 10:32 -0800
Re: Issue on assigning address to pointers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-17 18:45 +0000
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 10:53 -0800
Re: Issue on assigning address to pointers Keith Thompson <kst-u@mib.org> - 2015-11-17 15:05 -0800
Re: Issue on assigning address to pointers Philip Lantz <prl@canterey.us> - 2015-11-19 21:16 -0800
Re: Issue on assigning address to pointers Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2015-11-17 15:47 -0800
Re: Issue on assigning address to pointers Barry Schwarz <schwarzb@dqel.com> - 2015-11-17 15:53 -0800
Re: Issue on assigning address to pointers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-18 04:05 +0000
Re: Issue on assigning address to pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-17 19:41 +0000
Re: Issue on assigning address to pointers Stephen Sprunk <stephen@sprunk.org> - 2015-11-17 12:17 -0600
Re: Issue on assigning address to pointers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-17 18:21 +0000
Re: Issue on assigning address to pointers Geoff <geoff@invalid.invalid> - 2015-11-17 10:36 -0800
Re: Issue on assigning address to pointers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-17 18:46 +0000
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 10:49 -0800
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 10:47 -0800
Re: Issue on assigning address to pointers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-17 18:51 +0000
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 10:55 -0800
Re: Issue on assigning address to pointers Geoff <geoff@invalid.invalid> - 2015-11-17 11:59 -0800
Re: Issue on assigning address to pointers Stephen Sprunk <stephen@sprunk.org> - 2015-11-17 12:58 -0600
Re: Issue on assigning address to pointers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-17 19:12 +0000
Re: Issue on assigning address to pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-17 19:33 +0000
Re: Issue on assigning address to pointers Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-17 13:25 -0500
Re: Issue on assigning address to pointers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-17 18:31 +0000
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 10:39 -0800
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-17 10:40 -0800
Re: Issue on assigning address to pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-17 19:40 +0000
Re: Issue on assigning address to pointers Geoff <geoff@invalid.invalid> - 2015-11-17 11:15 -0800
Re: Issue on assigning address to pointers James Kuyper <jameskuyper@verizon.net> - 2015-11-17 15:32 -0500
Re: Issue on assigning address to pointers Keith Thompson <kst-u@mib.org> - 2015-11-17 15:09 -0800
Re: Issue on assigning address to pointers supercat@casperkitty.com - 2015-11-17 15:41 -0800
Re: Issue on assigning address to pointers Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-17 21:28 -0800
Re: Issue on assigning address to pointers supercat@casperkitty.com - 2015-11-17 23:18 -0800
Re: Issue on assigning address to pointers Geoff <geoff@invalid.invalid> - 2015-11-17 17:18 -0800
Re: Issue on assigning address to pointers James Kuyper <jameskuyper@verizon.net> - 2015-11-17 22:37 -0500
Re: Issue on assigning address to pointers Lőrinczy Zsigmond <zsiga@nospam.for.me> - 2015-11-18 11:27 +0100
Re: Issue on assigning address to pointers Keith Thompson <kst-u@mib.org> - 2015-11-18 06:13 -0800
Re: Issue on assigning address to pointers guinness.tony@gmail.com - 2015-11-24 02:46 -0800
Re: Issue on assigning address to pointers Keith Thompson <kst-u@mib.org> - 2015-11-24 09:21 -0800
Re: Issue on assigning address to pointers John Bode <jfbode1029@gmail.com> - 2015-11-24 05:01 -0800
Re: Issue on assigning address to pointers Keith Thompson <kst-u@mib.org> - 2015-11-24 09:22 -0800
Re: Issue on assigning address to pointers John Bode <jfbode1029@gmail.com> - 2015-11-24 06:47 -0800
Re: Issue on assigning address to pointers Alla _ <modelling.data@gmail.com> - 2015-11-25 06:44 -0800
csiph-web