Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #23515 > unrolled thread
| Started by | Bill Cunningham <billcun@suddenlink.net> |
|---|---|
| First post | 2012-07-21 14:14 -0700 |
| Last post | 2012-07-24 16:36 -0700 |
| Articles | 20 on this page of 32 — 13 participants |
Back to article view | Back to comp.lang.c
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
Page 1 of 2 [1] 2 Next page →
| From | Bill Cunningham <billcun@suddenlink.net> |
|---|---|
| Date | 2012-07-21 14:14 -0700 |
| Subject | qsort man page |
| Message-ID | <89644623-7e1c-4f8b-9f4d-6443526f0f73@u2g2000yqj.googlegroups.com> |
This is from the qsort man page. I don't quite understand why there is
a comment here saying that arguments to this generic function to pass
to qsort is a pointer to pointer. It looks to me like cmpstringp takes
a generic pointer. Not pointer to pointer. Why does the comment say
pointer to pointer? This is confusing me also with the strcmp
parameters which are cast as a pointer to pointer and dereferenced. I
hope I'm clear. THanks.
Bill
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int
cmpstringp(const void *p1, const void *p2)
{
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int
main(int argc, char *argv[])
{
int j;
if (argc < 2) {
fprintf(stderr, "Usage: %s <string>...\n", argv[0]);
exit(EXIT_FAILURE);
}
qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
for (j = 1; j < argc; j++)
puts(argv[j]);
exit(EXIT_SUCCESS);
}
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-07-21 22:57 +0100 |
| Message-ID | <0.7b858dd95d3281469ee0.20120721225709BST.87boj893re.fsf@bsb.me.uk> |
| In reply to | #23515 |
Bill Cunningham <billcun@suddenlink.net> writes:
> This is from the qsort man page. I don't quite understand why there is
> a comment here saying that arguments to this generic function to pass
> to qsort is a pointer to pointer. It looks to me like cmpstringp takes
> a generic pointer. Not pointer to pointer. Why does the comment say
> pointer to pointer?
Yup, cmpstringp takes two generic pointers, but in order to do anything
useful with them it must know to what kind of data they really point.
If the array being sorted is an array of ints, then the two pointers are
really pointing at int data, so casting to int * and dereferencing would
be the right thing to do (you'd write *(const int *)p1).
But, argv -- the array being sorted here -- is an array of character
pointers so the comparisson function gets passed pointers to a pair of
these character pointers, converted to "generic" (void *) pointers.
> This is confusing me also with the strcmp
> parameters which are cast as a pointer to pointer and dereferenced. I
> hope I'm clear. THanks.
>
> Bill
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> static int
> cmpstringp(const void *p1, const void *p2)
> {
> /* The actual arguments to this function are "pointers to
> pointers to char", but strcmp(3) arguments are "pointers
> to char", hence the following cast plus dereference */
> return strcmp(* (char * const *) p1, * (char * const *) p2);
> }
> int
> main(int argc, char *argv[])
> {
> int j;
> if (argc < 2) {
> fprintf(stderr, "Usage: %s <string>...\n", argv[0]);
> exit(EXIT_FAILURE);
> }
> qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
> for (j = 1; j < argc; j++)
> puts(argv[j]);
> exit(EXIT_SUCCESS);
> }
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Bill Cunningham <billcun@suddenlink.net> |
|---|---|
| Date | 2012-07-21 18:07 -0700 |
| Message-ID | <b00418d1-e717-4207-99d4-0ac12f2accba@googlegroups.com> |
| In reply to | #23524 |
On Saturday, July 21, 2012 5:57:09 PM UTC-4, Ben Bacarisse wrote:
> Bill Cunningham <billcun@suddenlink.net> writes:
>
> > This is from the qsort man page. I don't quite understand why there is
> > a comment here saying that arguments to this generic function to pass
> > to qsort is a pointer to pointer. It looks to me like cmpstringp takes
> > a generic pointer. Not pointer to pointer. Why does the comment say
> > pointer to pointer?
>
> Yup, cmpstringp takes two generic pointers, but in order to do anything
> useful with them it must know to what kind of data they really point.
I'm not quite sure I understand. I wanted to try this,
char name[]={"fred","suzy","mike","jennifer"};
And sort that using qsort. I know there are other functions that can be used with qsort like OT posix function but just speaking of C strcmp is the function to use.
Bill
> If the array being sorted is an array of ints, then the two pointers are
> really pointing at int data, so casting to int * and dereferencing would
> be the right thing to do (you'd write *(const int *)p1).
>
> But, argv -- the array being sorted here -- is an array of character
> pointers so the comparisson function gets passed pointers to a pair of
> these character pointers, converted to "generic" (void *) pointers.
>
> > This is confusing me also with the strcmp
> > parameters which are cast as a pointer to pointer and dereferenced. I
> > hope I'm clear. THanks.
[snip]
[toc] | [prev] | [next] | [standalone]
| From | Mark Storkamp <mstorkamp@yahoo.com> |
|---|---|
| Date | 2012-07-23 08:49 -0500 |
| Message-ID | <mstorkamp-527B11.08494123072012@news.eternal-september.org> |
| In reply to | #23537 |
In article <b00418d1-e717-4207-99d4-0ac12f2accba@googlegroups.com>,
Bill Cunningham <billcun@suddenlink.net> wrote:
> On Saturday, July 21, 2012 5:57:09 PM UTC-4, Ben Bacarisse wrote:
> > Bill Cunningham <billcun@suddenlink.net> writes:
> I'm not quite sure I understand. I wanted to try this,
>
> char name[]={"fred","suzy","mike","jennifer"};
>
> And sort that using qsort. I know there are other functions that can be used
> with qsort like OT posix function but just speaking of C strcmp is the
> function to use.
>
> Bill
Last November you were trying to sort numbers. Why don't you post the
code you got to work from back then. Then maybe someone can show you
what needs to be changed to make it work for your char array.
[toc] | [prev] | [next] | [standalone]
| From | Bill Cunningham <billcun@suddenlink.net> |
|---|---|
| Date | 2012-07-23 10:44 -0700 |
| Message-ID | <1545d2d7-1f70-4749-83ed-768eae0a7a5a@googlegroups.com> |
| In reply to | #23616 |
On Monday, July 23, 2012 9:49:41 AM UTC-4, Mark Storkamp wrote: > In article <b00418d1-e717-4207-99d4-0ac12f2accba@googlegroups.com>, > Last November you were trying to sort numbers. Why don't you post the > code you got to work from back then. Then maybe someone can show you > what needs to be changed to make it work for your char array. You have a very good memory. I remember asking about qsort but I didn't realize it was in November. Maybe I can look back and see what I got to work. Bill
[toc] | [prev] | [next] | [standalone]
| From | tom st denis <tom@iahu.ca> |
|---|---|
| Date | 2012-07-26 09:02 -0700 |
| Message-ID | <84084cfa-8b5c-43e4-9f6d-cbbf5caf1ecd@u2g2000yqj.googlegroups.com> |
| In reply to | #23623 |
On Jul 23, 1:44 pm, Bill Cunningham <bill...@suddenlink.net> wrote: > On Monday, July 23, 2012 9:49:41 AM UTC-4, Mark Storkamp wrote: > > In article <b00418d1-e717-4207-99d4-0ac12f2accba@googlegroups.com>, > > Last November you were trying to sort numbers. Why don't you post the > > code you got to work from back then. Then maybe someone can show you > > what needs to be changed to make it work for your char array. > > You have a very good memory. I remember asking about qsort but I didn't realize it was in November. Maybe I can look back and see what I got to work. > > Bill I'd like to state for the fact of stating that I knew how to call qsort() when I was 12 years old [18 something years ago...]. You're not stupider than a 12 year old are you? Tom
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2012-07-26 16:41 +0000 |
| Message-ID | <jurrv0$5j2$1@reader1.panix.com> |
| In reply to | #23744 |
In <84084cfa-8b5c-43e4-9f6d-cbbf5caf1ecd@u2g2000yqj.googlegroups.com> tom st denis <tom@iahu.ca> writes:
> I'd like to state for the fact of stating that I knew how to call
> qsort() when I was 12 years old [18 something years ago...]. You're
> not stupider than a 12 year old are you?
Some 12 year olds know how to milk a cow.
I don't know how to milk a cow, and I'd wager many of the folks in this
newsgroup don't either.
Does that make us stupider than a 12 year old?
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | tom st denis <tom@iahu.ca> |
|---|---|
| Date | 2012-07-26 09:59 -0700 |
| Message-ID | <63ec3ffa-aee9-4c12-8007-2b1a947f3589@6g2000vbv.googlegroups.com> |
| In reply to | #23745 |
On Jul 26, 12:41 pm, John Gordon <gor...@panix.com> wrote: > In <84084cfa-8b5c-43e4-9f6d-cbbf5caf1...@u2g2000yqj.googlegroups.com> tom st denis <t...@iahu.ca> writes: > > > I'd like to state for the fact of stating that I knew how to call > > qsort() when I was 12 years old [18 something years ago...]. You're > > not stupider than a 12 year old are you? > > Some 12 year olds know how to milk a cow. > > I don't know how to milk a cow, and I'd wager many of the folks in this > newsgroup don't either. > > Does that make us stupider than a 12 year old? If I spent 15 years trying to milk a cow and some 12 year old learned how to do it in a weekend I might consider myself in need of help. Tom
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@sverige.freeshell.org> |
|---|---|
| Date | 2012-07-23 14:43 +0000 |
| Message-ID | <slrn3vfsk0qoo4.cis.ike@sverige.freeshell.org> |
| In reply to | #23537 |
On 2012-07-22, Bill Cunningham <billcun@suddenlink.net> wrote:
> I'm not quite sure I understand. I wanted to try this,
>
> char name[]={"fred","suzy","mike","jennifer"};
The types of the array and the initializer do not match.
You probably want
char *name[] = {"fred","suzy","mike","jennifer"};
[toc] | [prev] | [next] | [standalone]
| From | Bill Cunningham <billcun@suddenlink.net> |
|---|---|
| Date | 2012-07-23 13:16 -0700 |
| Message-ID | <046684cd-59ad-49a7-a994-9a93c41db99f@googlegroups.com> |
| In reply to | #23618 |
On Monday, July 23, 2012 10:43:16 AM UTC-4, Ike Naar wrote:
> The types of the array and the initializer do not match.
> You probably want
>
> char *name[] = {"fred","suzy","mike","jennifer"};
Ok I see what your saying. I've never went as far as to get into pointers to pointers before.
B
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarz45@yahoo.com> |
|---|---|
| Date | 2012-07-23 13:29 -0700 |
| Message-ID | <92859c67-7050-4d92-bf9d-6993b884f892@googlegroups.com> |
| In reply to | #23630 |
On Monday, July 23, 2012 3:16:01 PM UTC-5, Bill Cunningham wrote:
> On Monday, July 23, 2012 10:43:16 AM UTC-4, Ike Naar wrote:
>
> > The types of the array and the initializer do not match.
> > You probably want
> >
> > char *name[] = {&quot;fred&quot;,&quot;suzy&quot;,&quot;mike&quot;,&quot;jennifer&quot;};
>
> Ok I see what your saying. I've never went as far as to get into pointers to pointers before.
>
> B
On Monday, July 23, 2012 3:16:01 PM UTC-5, Bill Cunningham wrote:
> On Monday, July 23, 2012 10:43:16 AM UTC-4, Ike Naar wrote:
>
> > The types of the array and the initializer do not match.
> > You probably want
> >
> > char *name[] = {&quot;fred&quot;,&quot;suzy&quot;,&quot;mike&quot;,&quot;jennifer&quot;};
>
> Ok I see what your saying. I've never went as far as to get into pointers to pointers before.
Since there are no pointers to pointers in the above code, why are you bringing it up as an additional confusion factor?
[toc] | [prev] | [next] | [standalone]
| From | Bill Cunningham <billcun@suddenlink.net> |
|---|---|
| Date | 2012-07-23 16:56 -0700 |
| Message-ID | <716bd8f4-8024-4e49-a66a-486408fd6aa5@googlegroups.com> |
| In reply to | #23631 |
On Monday, July 23, 2012 4:29:15 PM UTC-4, Barry Schwarz wrote: > Since there are no pointers to pointers in the above code, why are you bringing it up as an additional confusion factor? Barry the answer your looking for you answered in your own question just "...bringing it up as an additional confusion factor." Actually this code to me being char *name[]; looks equivalent to a char ** and a protoype of char ** would probably take char *name[]; B
[toc] | [prev] | [next] | [standalone]
| From | Noob <root@localhost> |
|---|---|
| Date | 2012-07-24 09:29 +0200 |
| Message-ID | <500e4ebc$0$1726$426a74cc@news.free.fr> |
| In reply to | #23639 |
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. In other contexts, arrays and pointers are NOT interchangeable!! Take a look at the recent thread: "Difference of extern short *x and extern short x[]?" (Date: Tue, 17 Jul 2012 14:10:45 +0200)
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-07-24 23:17 -0700 |
| Message-ID | <lnlii88ivp.fsf@nuthaus.mib.org> |
| In reply to | #23653 |
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"
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarz45@yahoo.com> |
|---|---|
| Date | 2012-07-25 09:51 -0700 |
| Message-ID | <847171d6-4e3b-4807-b290-bd501321a4a3@googlegroups.com> |
| In reply to | #23639 |
On Monday, July 23, 2012 6:56:13 PM UTC-5, Bill Cunningham wrote:
> On Monday, July 23, 2012 4:29:15 PM UTC-4, Barry Schwarz wrote:
>
> > Since there are no pointers to pointers in the above code, why are you bringing it up as an additional confusion factor?
>
> Barry the answer your looking for you answered in your own question just "...bringing it up as an additional confusion factor." Actually this code to me being char *name[]; looks equivalent to a char ** and a protoype of char ** would probably take char *name[];
Your comments make no sense and add further support to the conclusion you just add things at random.
For the benefit of any newcomer reading this thread, the object definitions:
char **x means x is a scalar object.
char *y[5] means y is an aggregate object.
parameter declarations in a function definition behave slightly differently due to special rules regarding arrays but that is a different topic.
[toc] | [prev] | [next] | [standalone]
| From | Bill Cunningham <billcun@suddenlink.net> |
|---|---|
| Date | 2012-07-25 20:51 -0700 |
| Message-ID | <a5511d7c-4a88-40d7-99a3-fb947cd66ae4@googlegroups.com> |
| In reply to | #23731 |
On Wednesday, July 25, 2012 12:51:10 PM UTC-4, Barry Schwarz wrote: > On Monday, July 23, 2012 6:56:13 PM UTC-5, Bill Cunningham wrote: > > On Monday, July 23, 2012 4:29:15 PM UTC-4, Barry Schwarz wrote: > > > > &gt; Since there are no pointers to pointers in the above code, why are you bringing it up as an additional confusion factor? > > > > Barry the answer your looking for you answered in your own question just &quot;...bringing it up as an additional confusion factor.&quot; Actually this code to me being char *name[]; looks equivalent to a char ** and a protoype of char ** would probably take char *name[]; > > Your comments make no sense and add further support to the conclusion you just add things at random. > > For the benefit of any newcomer reading this thread, the object definitions: > > char **x means x is a scalar object. > > char *y[5] means y is an aggregate object. > > parameter declarations in a function definition behave slightly differently due to special rules regarding arrays but that is a different topic. Scalar or aggregate object? Huh? I'm not touching this one. This post isn't addressed to me anyway it might be best just to abandon this thread.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarz45@yahoo.com> |
|---|---|
| Date | 2012-07-26 14:27 -0700 |
| Message-ID | <4b551c4e-fc70-410b-b946-a46039b7c01e@googlegroups.com> |
| In reply to | #23735 |
On Wednesday, July 25, 2012 10:51:12 PM UTC-5, Bill Cunningham wrote: > Scalar or aggregate object? Huh? I'm not touching this one. This post isn't addressed to me anyway it might be best just to abandon this thread. Thios is a Usenet discussion group. Posts are not addressed to individuals at all. If you are willing to abandon the thread without ever demonstrating that you have understood the answers to your original question, in fact just the opposite, one has to wonder why you bothered to ask in the first place.
[toc] | [prev] | [next] | [standalone]
| From | Bill Cunningham <billcun@suddenlink.net> |
|---|---|
| Date | 2012-07-26 18:20 -0700 |
| Message-ID | <17ef919b-1663-4e73-b567-cd5c9472a7d8@googlegroups.com> |
| In reply to | #23763 |
On Thursday, July 26, 2012 5:27:57 PM UTC-4, Barry Schwarz wrote: > Thios is a Usenet discussion group. Posts are not addressed to individuals at all. > > If you are willing to abandon the thread without ever demonstrating that you have understood the answers to your original question, in fact just the opposite, one has to wonder why you bothered to ask in the first place. I doubt that I could ever demonstrate to clc that I have the answers to the question even if I did. clc isn't exactly a learning group. Not enough patience. This is getting too technical. Doomed to failure. Thanks anyway.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarz45@yahoo.com> |
|---|---|
| Date | 2012-07-28 07:24 -0700 |
| Message-ID | <ceb425d1-1fa8-4330-829f-a5666da9b7c9@googlegroups.com> |
| In reply to | #23776 |
On Thursday, July 26, 2012 8:20:07 PM UTC-5, Bill Cunningham wrote: > On Thursday, July 26, 2012 5:27:57 PM UTC-4, Barry Schwarz wrote: > > > > > Thios is a Usenet discussion group. Posts are not addressed to individuals at all. > > > > > > If you are willing to abandon the thread without ever demonstrating that you have understood the answers to your original question, in fact just the opposite, one has to wonder why you bothered to ask in the first place. > > > > I doubt that I could ever demonstrate to clc that I have the answers to the question even if I did. clc isn't exactly a learning group. Not enough patience. This is getting too technical. Doomed to failure. Thanks anyway. If you didn't want a technical answer, why are you posting to a newsgroup that deals with the technical aspects of the language?
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2012-07-23 23:39 -0400 |
| Message-ID | <500E18E3.19CA@mindspring.com> |
| In reply to | #23618 |
Ike Naar wrote:
>
> On 2012-07-22, Bill Cunningham <billcun@suddenlink.net> wrote:
> > I'm not quite sure I understand. I wanted to try this,
> >
> > char name[]={"fred","suzy","mike","jennifer"};
>
> The types of the array and the initializer do not match.
> You probably want
>
> char *name[] = {"fred","suzy","mike","jennifer"};
/* BEGIN new.c */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NMEMB(A) (sizeof (A) / sizeof *(A))
int cmpstringp(const void *p1, const void *p2);
int
main(void)
{
size_t element;
char *name[] = {"fred","suzy","mike","jennifer"};
qsort(name, NMEMB(name), sizeof *name, cmpstringp);
for (element = 0; element != NMEMB(name); ++element) {
puts(name[element]);
}
return 0;
}
int
cmpstringp(const void *p1, const void *p2)
{
const char *const *const pp1 = p1;
const char *const *const pp2 = p2;
return strcmp(*pp1, *pp2);
}
/* END new.c */
--
pete
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web