Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Pointers: misunderstanding |
| Date | 2015-11-06 13:06 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87d1vnxooe.fsf@bsb.me.uk> (permalink) |
| References | <982998f5-5ccc-4bbb-9c57-281252c80a8a@googlegroups.com> <MPG.30a62c02fd25cea07e@news.eternal-september.org> <4b008308-c735-4da8-b946-533409f02cc5@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
> On Friday, November 6, 2015 at 3:17:06 PM UTC+4, Philip Lantz wrote:
>> Alla _ wrote:
>>
>> > I have to write a pointer version of a sort () function from
>> > this program:
>>
>> I would interpret this description to mean that the sort routine should
>> sort an array of pointers to the data, rather than an array of the data
>> itself.
>
> I guess that it reflects what the author of the task meant.
Sure but I, for one, want to know what was meant by the author! Philip
has introduced another possibility (which I also considered) but it's
not what you've been doing.
>> This is a useful technique if the data to be sorted is large, because it
>> is way faster to swap pointers to a chunk of data that to copy the data
>> itself.
>>
> yes, that is what I have learned these days :)
I've not seen anything in the recent discussion that could have lead to
to that conclusion. It's correct (so well done if you have worked out
the value of what is sometimes called indirect sorting) but it has
nothing to do with what you have been doing, which it re-writing indexed
loops with pointer loops.
> Also, according to what Ben has suggested (I am not sure I have understood
> it clearily), and what Barry has explained, I have tried to rewrite
> the sort function. The program now works.
>
> But what I still not clear to me is why
>
> void sort (int* array_int, int n) is different
>
> from void sort (int array_int, int n), if in both cases
You mean void sort (int array_int[], int n)
> array_int is a pointer to the address of the first element
> in the given array. Sorry for being slow in understanding
> these basics.
There is no difference. None at all. For various historical reasons
and accidents of design, they mean exactly the same thing.
But did you read the declaration that Philip Lantz wrote carefully? He
was suggesting something else entirely:
void sort(int *array_int[], int n);
> // Program to sort an array of integers into ascending order
>
> #include <stdio.h>
>
> void sort (int* array_int, int n)
> {
> int* temp;
> int* pointer_1 = array_int;
> int* array_end = &array_int[n - 1];
When n is zero, this is not a permitted operation. Worse, you then
subtract 1 from array_end in the while loop. C has a funny rule that
you can make pointers to the elements of an array, and also a pointer
just past the end (there's no element there, but you can make the
pointer) but no other pointers can even be constructed.
> int* pointer_2;
>
> while (pointer_1 < array_end - 1)
You two loops are very similar. Whatever you do, you should write them
both in the same style. Both with for or both with while. And if you
use for, both for loops should have the same shape.
> {
> for (pointer_2 = pointer_1 + 1; pointer_2 < array_end; pointer_2++)
> {
> if (*pointer_1 > *pointer_2)
> {
> *temp = *pointer_1;
EEK! temp is a pointer you have not set to point anywhere. *temp is
therefore what we call "undefined behaviour". But what type is
*pointer_1? What sort of variable should you use to hold something of
that type?
> *pointer_1 = *pointer_2;
> *pointer_2 = *temp;
> }
> pointer_1++;
> }
> }
>
> }
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-05 09:09 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-05 17:44 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 00:49 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 03:01 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 03:09 -0800
Re: Pointers: misunderstanding Barry Schwarz <schwarzb@dqel.com> - 2015-11-05 11:40 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 03:08 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 11:33 +0000
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-06 08:53 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 03:16 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 03:30 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 03:32 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 04:58 -0800
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-06 09:00 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:25 -0800
Re: Pointers: misunderstanding supercat@casperkitty.com - 2015-11-06 10:07 -0800
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-06 12:31 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 20:08 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 04:59 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 13:06 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 08:50 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 17:00 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:17 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 17:22 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:29 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 17:53 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:22 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 17:24 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:33 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 18:29 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 23:42 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 20:24 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 17:51 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 23:37 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-07 13:36 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-07 08:17 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-07 12:04 -0800
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-06 08:55 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:23 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 03:26 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 23:48 -0800
Re: Pointers: misunderstanding Barry Schwarz <schwarzb@dqel.com> - 2015-11-07 00:35 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 23:48 -0800
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-07 12:24 -0800
csiph-web