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: Program to sort three integers |
| Date | 2015-11-06 16:45 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <n1ilap$85f$1@dont-email.me> (permalink) |
| References | <8dcd1ed2-2734-4cff-b013-d713a33100ed@googlegroups.com> |
On 06/11/15 16:20, Alla _ wrote:
> Hello!
>
> Here is the description of the task:
>
> Write a function called sort() to sort three integers into ascending order. (This function is not to be implemented with arrays.)
Okay, let's start off by implementing it correctly, so that we have
something to compare against:
#include <stdio.h>
void swap(int *left, int *right)
{
int tmp = *left;
*left = *right;
*right = tmp;
}
void sort(int *a, int *b, int *c)
{
if(*a > *b)
{
swap(a, b);
}
/* The value at *a is now smaller than that at *b */
if(*b > *c)
{
swap(b, c);
}
/* The value at *c is now definitely the biggest, but *b
could now hold a value smaller than *a, so check again. */
if(*a > *b)
{
swap(a, b);
}
}
int main(void)
{
int i = 3;
int j = 1;
int k = 9; /* your own data */
printf("Before: %d %d %d\n", i, j, k);
sort(&i, &j, &k);
printf(" After: %d %d %d\n", i, j, k);
return 0;
}
Result:
Before: 3 1 9
After: 1 3 9
So now let's take a look at your code:
<snip>
> Allas-MacBook-Pro:ch10 Alla$ gcc -Wall -Wextra -std=c99 -pedantic -o ex10-8 ex10-8.c
When I compiled this, I was told:
alla.c: In function ‘sort’:
alla.c:72:11: warning: ‘value_3’ is used uninitialized in this function
[-Wuninitialized]
printf("%i %i %i\n", *value_1, *value_2, *value_3);
^
alla.c:72:11: warning: ‘value_2’ is used uninitialized in this function
[-Wuninitialized]
alla.c:72:11: warning: ‘value_1’ is used uninitialized in this function
[-Wuninitialized]
And that's your problem right there.
If you kick it hard enough, the compiler will give you all kinds of
useful information.
> Allas-MacBook-Pro:ch10 Alla$ ./ex10-8
> Bus error: 10
>
> Second version has printf within the main function, also compiles, and
> produces the same bus error.
>
> 1)
> // Program to sort three integers using pointers
>
> #include <stdio.h>
>
> void sort (int* a, int* b, int* c);
>
> int main(void)
> {
> int one = 3;
> int two = 1;
> int three = 9;
>
> sort(&one, &two, &three);
>
> return 0;
> }
>
> void sort (int* a, int* b, int* c)
> {
> int* value_1;
> int* value_2;
> int* value_3;
These are pointers-to-int. You can use them for pointing to ints, but at
present you haven't pointed them anywhere. Their values are
"indeterminate", which means we don't know what those values are. That
is, we have no guarantee that they are pointing anywhere useful or safe.
> if (*a < *b)
> {
> if (*a < *c)
> {
> *value_1 = *a;
So, here, we take the value of the int pointed to by a, and copy it
into... where? value_1 isn't pointing to any int that we own. This is
almost certainly the reason for your "bus error". Whatever indeterminate
value is scribbled into that object is not aligned correctly for
addressing an int. (It may not be this one, of course - it might be
*value_2 or *value_3, but certainly at least one of these is the culprit.)
Never Write Through A Pointer Unless You Know Where It Is Pointing!
> 2)
> // Program to sort three integers using pointers
<snip>
> int* value_1;
> int* value_2;
> int* value_3;
>
> if (*a < *b)
> {
> if (*a < *c)
> {
> *value_1 = *a;
Same problem.
> Truly unexpected: by now I should have already been able to solve
> such easy tasks.
You made it too complicated, that's all. Why did you make value_1,
value_2, and value_3 pointers? What was wrong with making them ints?
> I am doing my best to get too disappointed with
> myself, and I do hope for your patience and help.
You're doing fine, but you need to go slower and listen more.
> Another important issue which both versions have revealed again:
> I do not understand how to make a function return several values;
Neither do I. If you find out, let me know.
Actually, there is a way, using an aggregate data structure, or better
still a pointer to it, but I wouldn't bother worrying about that for
now. At present, you have more important things to worry about.
--
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
Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-06 08:20 -0800
Re: Program to sort three integers Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-06 11:32 -0500
Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 00:20 -0800
Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-07 11:04 +0000
Re: Program to sort three integers "Osmium" <r124c4u102@comcast.net> - 2015-11-06 10:34 -0600
Re: Program to sort three integers "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-06 08:37 -0800
Re: Program to sort three integers supercat@casperkitty.com - 2015-11-06 10:36 -0800
Re: Program to sort three integers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 19:17 +0000
Re: Program to sort three integers supercat@casperkitty.com - 2015-11-06 11:30 -0800
Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 01:01 -0800
Re: Program to sort three integers "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-06 11:48 -0800
Re: Program to sort three integers David Brown <david.brown@hesbynett.no> - 2015-11-06 23:15 +0100
Re: Program to sort three integers "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-11-06 14:39 -0800
Re: Program to sort three integers Keith Thompson <kst-u@mib.org> - 2015-11-06 15:57 -0800
Re: Program to sort three integers Nick Bowler <nbowler@draconx.ca> - 2015-11-06 22:33 +0000
Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 16:39 +0000
Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 00:22 -0800
Re: Program to sort three integers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 16:45 +0000
Re: Program to sort three integers "Bill Davy" <Bill@SynectixLtd.com> - 2015-11-06 18:30 +0000
Re: Program to sort three integers Keith Thompson <kst-u@mib.org> - 2015-11-06 11:36 -0800
Re: Program to sort three integers Bartc <bc@freeuk.com> - 2015-11-06 19:50 +0000
Re: Program to sort three integers Keith Thompson <kst-u@mib.org> - 2015-11-06 12:52 -0800
Re: Program to sort three integers Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-11-09 13:36 +0000
Re: Program to sort three integers David Brown <david.brown@hesbynett.no> - 2015-11-09 15:41 +0100
Re: Program to sort three integers raltbos@xs4all.nl (Richard Bos) - 2015-11-10 16:56 +0000
Re: Program to sort three integers Keith Thompson <kst-u@mib.org> - 2015-11-10 09:11 -0800
Re: Program to sort three integers supercat@casperkitty.com - 2015-11-10 09:15 -0800
Re: Program to sort three integers Les Cargill <lcargill99@comcast.com> - 2015-11-10 19:37 -0600
Re: Program to sort three integers asetofsymbols@gmail.com - 2015-11-06 11:52 -0800
Re: Program to sort three integers Bartc <bc@freeuk.com> - 2015-11-06 20:01 +0000
Re: Program to sort three integers asetofsymbols@gmail.com - 2015-11-06 12:21 -0800
Re: Program to sort three integers "Osmium" <r124c4u102@comcast.net> - 2015-11-06 14:23 -0600
Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 20:58 +0000
Re: Program to sort three integers Mark Storkamp <mstorkamp@yahoo.com> - 2015-11-06 16:04 -0600
Re: Program to sort three integers Mark Storkamp <mstorkamp@yahoo.com> - 2015-11-06 16:08 -0600
Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 22:49 +0000
Re: Program to sort three integers Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-10 16:04 -0800
Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-11 21:51 +0000
Re: Program to sort three integers bartekltg <bartekltg@gmail.com> - 2015-11-11 23:50 +0100
Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 22:52 +0000
Re: Program to sort three integers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-07 01:17 +0000
Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 01:24 -0800
Re: Program to sort three integers Chad <cdalten@gmail.com> - 2015-11-07 02:04 -0800
Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 00:57 -0800
Re: Program to sort three integers Barry Schwarz <schwarzb@dqel.com> - 2015-11-06 10:14 -0800
Re: Program to sort three integers Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 18:27 +0000
Re: Program to sort three integers Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-06 11:22 -0800
Re: Program to sort three integers Alla _ <modelling.data@gmail.com> - 2015-11-07 01:15 -0800
Re: Program to sort three integers Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-07 00:57 -0800
Re: Program to sort three integers bartekltg <bartekltg@gmail.com> - 2015-11-10 02:07 +0100
csiph-web