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


Groups > comp.lang.c > #23708

Re: qsort man page

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: qsort man page
Date 2012-07-24 23:17 -0700
Organization None to speak of
Message-ID <lnlii88ivp.fsf@nuthaus.mib.org> (permalink)
References (3 earlier) <slrn3vfsk0qoo4.cis.ike@sverige.freeshell.org> <046684cd-59ad-49a7-a994-9a93c41db99f@googlegroups.com> <92859c67-7050-4d92-bf9d-6993b884f892@googlegroups.com> <716bd8f4-8024-4e49-a66a-486408fd6aa5@googlegroups.com> <500e4ebc$0$1726$426a74cc@news.free.fr>

Show all headers | View raw


Noob <root@localhost> writes:
> Bill Cunningham wrote:
>
>> [...] char *name[]; looks equivalent to a char ** and a protoype
>> of char ** would probably take char *name[];
>
> "Array of foo" and "pointer to foo" are only "equivalent"
> when used as function parameters, because only then does
> an array "decay" into a pointer to its first element.

Incorrect.

There are actually two different things going on here.

An expression of array type is converted to a pointer to the array
object's first element in *most* contexts, not just when used as
a function argument (i.e., an expression in a function call).  The
exceptions are:

- When the array expression is the operand of a unary "&" operator
  (which yields the address of the entire array object, not the address
  of the address of the first element -- which wouldn't even make
  sense);
- When the array expression is the operand of a unary "sizeof" operator
  (which yields the size of the entire array, not the size of a pointer;
  and
- When the array expression is a string literal in an initializer, used
  to initialize an array object (for example,
      char s[6] = "hello";
  copies the entire array into s).

The other thing is that a function parameter defined as "array
of foo" is "adjusted" to a pointer to foo.  This is a compile-time
transformation, not a conversion.  Perhaps you were using the word
"decay" to refer to this adjustment, but that word is more often
used to refer to the conversion I described -- though the standard
doesn't use the word "decay", at least not in that sense.

These two rules conspire to make people *think* that arrays and pointers
are interchangeable.

See also, as always, section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.

> In other contexts, arrays and pointers are NOT interchangeable!!

Correct.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"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

qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-21 14:14 -0700
  Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-21 22:57 +0100
    Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-21 18:07 -0700
      Re: qsort man page Mark Storkamp <mstorkamp@yahoo.com> - 2012-07-23 08:49 -0500
        Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-23 10:44 -0700
          Re: qsort man page tom st denis <tom@iahu.ca> - 2012-07-26 09:02 -0700
            Re: qsort man page John Gordon <gordon@panix.com> - 2012-07-26 16:41 +0000
              Re: qsort man page tom st denis <tom@iahu.ca> - 2012-07-26 09:59 -0700
      Re: qsort man page Ike Naar <ike@sverige.freeshell.org> - 2012-07-23 14:43 +0000
        Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-23 13:16 -0700
          Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-23 13:29 -0700
            Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-23 16:56 -0700
              Re: qsort man page Noob <root@localhost> - 2012-07-24 09:29 +0200
                Re: qsort man page Keith Thompson <kst-u@mib.org> - 2012-07-24 23:17 -0700
              Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-25 09:51 -0700
                Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-25 20:51 -0700
                Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-26 14:27 -0700
                Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-26 18:20 -0700
                Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-28 07:24 -0700
        Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-23 23:39 -0400
    Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-22 00:11 -0400
      Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-22 14:41 +0100
        Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-22 13:01 -0400
          Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-22 21:36 +0100
            Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-23 22:40 -0400
              Re: qsort man page pacman@kosh.dhis.org (Alan Curry) - 2012-07-24 04:29 +0000
                Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-24 11:40 +0100
                Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-24 22:06 -0400
        Re: qsort man page Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 11:38 -0700
  Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-23 23:34 -0400
    Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-24 00:07 -0400
  Re: qsort man page "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-07-24 16:36 -0700

csiph-web