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-05 17:44 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87twp0z6gz.fsf@bsb.me.uk> (permalink) |
| References | <982998f5-5ccc-4bbb-9c57-281252c80a8a@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
> I have to write a pointer version of a sort () function from
> this program:
I'm not sure what that means. Was anything else said? I think you are
being asker to get rid of the numerical indexes, i and j. The reason
I'm a bit confused is that the version you were given uses pointers:
> // Program to sort an array of integers into ascending order
>
> #include <stdio.h>
>
> void sort (int a[], int n)
'a' is a pointer.
> {
> int i, j, temp;
>
> for (i = 0; i < n-1; i++)
> for (j = i + 1; j < n; j++)
> if (a[i] > a[j])
and these are just pointer arithmetic and de-reference hidden behind []s.
> {
> temp = a[i];
> a[i] = a[j];
> a[j] = temp;
> }
> }
<snip>
> I have failed to produce a correct version, although I thought
> that I have understood everything I've read on pointers.
>
> Please, take a look at my version and my explanations. What am
> I misunderstanding?
> This is the incorrect result I get:
>
> The array before the sort:
> 34 -5 6 0 12 100
> The array after the sort:
> -5 6 0 12 34 6
>
> // Program to sort an array of integers into ascending order
>
> #include <stdio.h>
>
> void sort (int* array_int, int n)
>
> /** is equivalent to void sort (int array_int, int n) because
No! It's just a typo, but if you are going to write comments you need
to take care with them.
> when the name of the variable, which contains an array, is used
> as an argument of a function, then the compiler treats this name
> as a pointer to the array **/
It does so in almost every single case. It's more important to know the
exceptions rather than state the rule. The two you will need to know
(eventually) are sizeof array and &array.
> {
> int* temp;
> int* pointer = array_int;
>
> /** variable 'pointer' points to the address of the first element
> in array_int **/
>
> int* array_end = &array_int[n + 1];
>
> /** array_end points to the element beyond the last element in the
> array_int;
No. It's an invalid pointer pointing one further than that. Imagine
and array with, say, 3 elements. The first is &array_int[0], the second
is &array_int[1] and the last is &array_int[2]. A pointer just after
the end of the array is &array_int[3]. Note the 3 there -- that's your
n. &array_int[4] is too far.
> I have also tried to use 'int* array_end = *(array_int + n);'**/
*(array_int + n) means array_int[n]. That's an int. You can't assign
and it to a pointer. Maybe you meant array_int + n? Or even the rather
complex (but valid) &*(array_int + n)?
> /** iterate through entire array **/
>
> for (pointer; pointer < array_end; pointer++)
It's not usual to put the first unnecessary expression there. I'd write
any of:
while (pointer < array_end) { ... // and then add ++pointer in the loop }
for (; pointer < array_end; pointer++)
for (pointer = array_int; pointer < array_end; pointer++)
for (int *pointer = array_int; pointer < array_end; pointer++)
> {
>
> /** if the value at a certain position in the array, to which variable
> 'pointer' points to, is bigger then the next value in the array, to which
> (pointer + 1) points to, then swap those values **/
>
> if (*pointer > *(pointer + 1))
I'd write pointer[1] rather than *(pointer + 1) but that just a style
thing.
Much more importantly, you have a sort function with one loop. It makes
just one single pass over the array. The original had a loop in a loop
so there's is no way your single pass can be doing the same work as the
original.
> {
> *temp = *pointer;
> *pointer = *(pointer + 1);
> *(pointer + 1) = *temp;
> }
> }
> }
<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