Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #9484

Re: selection-sort in C

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: selection-sort in C
Date 2011-08-16 09:27 -0700
Organization None to speak of
Message-ID <lnr54l1fui.fsf@nuthaus.mib.org> (permalink)
References <4e4a4fc9$0$313$14726298@news.sunsite.dk> <871uwlk39e.fsf@bazspaz.fatphil.org> <4e4a563d$0$313$14726298@news.sunsite.dk> <0.8ac9a68b92bf7919228a.20110816132259BST.87pqk5eeak.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> arnuld <sunrise@invalid.address> writes:
>>> On Tue, 16 Aug 2011 14:24:45 +0300, Phil Carmody wrote:
[...]
>>> The consts on those parameters don't do much, but aren't actually wrong.
>>
>> Just to show that function is not supposed to change its arguments.
>
> The terminology is wrong here.  No C function can change its
> arguments -- arguments are the things you pass to a function.  Your
> declarations tell the reader that the function does not change its
> *parameters*.

(I think the attribution levels got messed up a bit.)

Here's a program that illustrates the distiction:

#include <stdio.h>

void func_with_const(const int param)
{
    printf("In func_with_const, param = %d\n", param);
    /* param = 100; */ /* would be illegal */
    *(int*)&param = 200; /* undefined behavior! */
    printf("    Now param = %d\n", param);
}

void func_without_const(int param)
{
    printf("In func_with_const, param = %d\n", param);
    param = 300; /* perfectly legal */
    printf("    Now param = %d\n", param);
}

int main(void)
{
    int argument = 10;
    printf("In main, argument = %d\n", argument);
    func_with_const(argument);
    func_without_const(argument);
    printf("Back in main, argument is still %d\n", argument);
    return 0;
}

In func_with_const(), the "const" on the parameter declaration is a
promise not to modify the value of the parameter, which is a local
variable to the function.  It's relevant only within the body of the
function itself, and is of no interest to the caller.  In this case, the
fucntion uses a pointer cast to violate that promise, but even that
doesn't affect the argument.

func_without_const() doesn't promise not to modify its parameter, but
again, the modification has no effect on the caller.

You could have a *declaration* like
    void func1(int param);
and a *definition* like
    void func1(const int param) { ... param = ...; ... }
so the promise is made only where it's relevant, but it's questionable
whether that's worth the effort and potential confusion.

Note that "const" *can* meaningfully appear in a pointer parameter
declaration:

    void func2(const int *param);

Here the "const" applies to the object (if any) that the parameter
points to, not to the parameter itself.  A caller can pass func2() a
pointer to some data with the assurance that the pointed-to data won't
be modified.

You could make the parameter itself read-only like this:

    void func3(int *const param);

but, like the "const param" declaration, this isn't meaningful for the
caller.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

selection-sort in C arnuld <sunrise@invalid.address> - 2011-08-16 11:08 +0000
  Re: selection-sort in C Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-08-16 14:24 +0300
    Re: selection-sort in C arnuld <sunrise@invalid.address> - 2011-08-16 11:36 +0000
      Re: selection-sort in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-08-16 13:22 +0100
        Re: selection-sort in C arnuld <sunrise@invalid.address> - 2011-08-16 13:44 +0000
          Re: selection-sort in C Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2011-08-16 07:36 -0700
        Re: selection-sort in C Thomas Boell <tboell@domain.invalid> - 2011-08-16 18:03 +0200
          Re: selection-sort in C karnath <karnathtorjian@gmail.com> - 2011-08-16 16:25 +0000
            Re: selection-sort in C Willem <willem@toad.stack.nl> - 2011-08-16 16:33 +0000
          Re: selection-sort in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-08-16 20:03 +0100
        Re: selection-sort in C Keith Thompson <kst-u@mib.org> - 2011-08-16 09:27 -0700
      Re: selection-sort in C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-17 20:17 +0000
        Re: selection-sort in C pete <pfiland@mindspring.com> - 2011-08-18 00:41 -0400
          Re: selection-sort in C Ben Pfaff <blp@cs.stanford.edu> - 2011-08-17 21:52 -0700
            Re: selection-sort in C pete <pfiland@mindspring.com> - 2011-08-18 01:07 -0400
              Re: selection-sort in C blp@cs.stanford.edu (Ben Pfaff) - 2011-08-17 22:36 -0700
                Re: selection-sort in C pete <pfiland@mindspring.com> - 2011-08-18 07:00 -0400
    Re: selection-sort in C BGB <cr88192@hotmail.com> - 2011-08-16 14:41 -0700
      Re: selection-sort in C Ian Collins <ian-news@hotmail.com> - 2011-08-17 10:10 +1200
        Re: selection-sort in C BGB <cr88192@hotmail.com> - 2011-08-16 17:25 -0700
          Re: selection-sort in C pacman@kosh.dhis.org (Alan Curry) - 2011-08-17 01:53 +0000
  Re: selection-sort in C Gene <gene.ressler@gmail.com> - 2011-08-16 05:27 -0700
  Re: selection-sort in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-08-16 07:24 -0700
  Re: selection-sort in C pete <pfiland@mindspring.com> - 2011-08-16 19:22 -0400
    Re: selection-sort in C arnuld <sunrise@invalid.address> - 2011-08-17 07:52 +0000
      Re: selection-sort in C Angel <angel+news@spamcop.net> - 2011-08-17 10:15 +0000
      Re: selection-sort in C Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-08-17 13:22 +0300
      Re: selection-sort in C pete <pfiland@mindspring.com> - 2011-08-18 00:33 -0400

csiph-web