473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C Pointers

How do I sort an string array using pointers
Dec 1 '07 #1
33 1915
pa*******@yahoo.com wrote:
How do I sort an string array using pointers
How do you think? Post an attempt and you will get help.

--
Ian Collins.
Dec 1 '07 #2
pa*******@yahoo.com writes:
How do I sort an string array using pointers
Tip: qsort + strcmp

Have a nice day of C programming
Friedrich

--
Please remove just-for-news- to reply via e-mail.
Dec 1 '07 #3
On 12ÔÂ1ÈÕ, ÏÂÎç4ʱ07·Ö, Friedrich Dominicus <just-for-news-fr...@q-software-
solutions.dewrote:
pateld...@yahoo.com writes:
How do I sort an string array using pointers

Tip: qsort + strcmp

Have a nice day of C programming
Friedrich

--
Please remove just-for-news- to reply via e-mail.
why not try www.google.com?
have a nice day:)
Dec 1 '07 #4
Friedrich Dominicus wrote:
pa*******@yahoo.com writes:
>How do I sort an string array using pointers

Tip: qsort + strcmp

Have a nice day of C programming
Simpler: Make a singly linked list and apply merge sort.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 1 '07 #5
CBFalconer wrote:
Friedrich Dominicus wrote:
>pa*******@yahoo.com writes:
>>How do I sort an string array using pointers

Tip: qsort + strcmp

Have a nice day of C programming

Simpler: Make a singly linked list and apply merge sort.
How is this method simpler?

Dec 1 '07 #6
On Nov 30, 9:44 pm, Ian Collins <ian-n...@hotmail.comwrote:
pateld...@yahoo.com wrote:
How do I sort an string array using pointers

How do you think? Post an attempt and you will get help.

--
Ian Collins.
what about this attempt

void sort(char *m[10])
{
int a,b,x;
char *temp;

for(a=0;a < 10;a++)
for(b = a + 1;b<10;b++)
{
x=0;
while(*(*(m+a) +x))
{
if( *(*(m+a)+x) *(*(m + b)+x) )
{
temp = *(m+a);
*(m+a) = *(m +b);
*(m + b) = temp;
break;
}
else if( *(*(m+a)+x) < *(*(m+b)+x) )
break;

else
x++;
}
}
}
Dec 1 '07 #7
aa*****@gmail.com wrote:
On Nov 30, 9:44 pm, Ian Collins <ian-n...@hotmail.comwrote:
>pateld...@yahoo.com wrote:
How do I sort an string array using pointers

How do you think? Post an attempt and you will get help.

--
Ian Collins.

what about this attempt

void sort(char *m[10])
{
int a,b,x;
char *temp;

for(a=0;a < 10;a++)
for(b = a + 1;b<10;b++)
{
x=0;
while(*(*(m+a) +x))
{
if( *(*(m+a)+x) *(*(m + b)+x) )
{
temp = *(m+a);
*(m+a) = *(m +b);
*(m + b) = temp;
break;
}
else if( *(*(m+a)+x) < *(*(m+b)+x) )
break;

else
x++;
}
}
}
Please do submit this to:

<http://ioccc.org/>

Dec 1 '07 #8
On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
How do I sort an string array using pointers
#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() 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 1) {
qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
for (j = 1; j < argc; j++)
puts(argv[j]);
}
exit(EXIT_SUCCESS);
}
Dec 1 '07 #9
vi******@gmail.com wrote:
>
On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
How do I sort an string array using pointers

#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() arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);
The type of your strcmp arguments is (char *const).
The strcmp parameter type is (const char *).

This way would be more better:

return strcmp(* (const char **) p1, * (const char **) p2);
}

int
main(int argc, char *argv[])
{
int j;
if(argc 1) {
qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
for (j = 1; j < argc; j++)
puts(argv[j]);
}
exit(EXIT_SUCCESS);
}
--
pete
Dec 1 '07 #10
On Sat, 01 Dec 2007 07:06:31 -0500, pete wrote:
vi******@gmail.com wrote:
>>
On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
How do I sort an string array using pointers

#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() arguments are "pointers to
char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);

The type of your strcmp arguments is (char *const). The strcmp parameter
type is (const char *).
char * can be implicitly converted to const char *.
This way would be more better:

return strcmp(* (const char **) p1, * (const char **) p2);
argv is declared as char *[]. argv[n] (which is what p1 and p2 point to)
is a char *. You shouldn't access a char * as if it were a const char *.

I'm not saying it's not allowed, because I'm not sure about that, but I
consider it poor style at least.
Dec 1 '07 #11
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
>
On Sat, 01 Dec 2007 07:06:31 -0500, pete wrote:
vi******@gmail.com wrote:
>
On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
How do I sort an string array using pointers

#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() arguments are "pointers to
char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);
The type of your strcmp arguments is (char *const). The strcmp parameter
type is (const char *).

char * can be implicitly converted to const char *.
This way would be more better:

return strcmp(* (const char **) p1, * (const char **) p2);

argv is declared as char *[].
argv[n] (which is what p1 and p2 point to) is a char *.
You shouldn't access a char * as if it were a const char *.

I'm not saying it's not allowed, because I'm not sure about that,
but I consider it poor style at least.
However,
strcmp *will* access its arguments as type (const char *).
A function call to strcmp,
implies that there are parameters of type (const char *)
being initialized with the values of the arguments.
The values of those arguments should be type (const char *).
int strcmp(const char *s1, const char *s2);
--
pete
Dec 1 '07 #12
On Sat, 01 Dec 2007 07:32:04 -0500, pete wrote:
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
>On Sat, 01 Dec 2007 07:06:31 -0500, pete wrote:
vi******@gmail.com wrote:

On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
How do I sort an string array using pointers

#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() arguments are "pointers to
char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);

The type of your strcmp arguments is (char *const). The strcmp
parameter type is (const char *).

char * can be implicitly converted to const char *.
This way would be more better:

return strcmp(* (const char **) p1, * (const char **) p2);

argv is declared as char *[].
argv[n] (which is what p1 and p2 point to) is a char *. You shouldn't
access a char * as if it were a const char *.

I'm not saying it's not allowed, because I'm not sure about that, but I
consider it poor style at least.

However,
strcmp *will* access its arguments as type (const char *).
No, it won't. It will access its parameters as type (const char *), and
its parameters *are* type const char *, as the result of the implicit
conversion from the arguments (of type char *) to const char *. You can
compare it to this:

Good:

char a = 'x';
int b = a;
const int *c = &b;

Bad:

char a = 'x';
const int *c = (int *) &a;
Dec 1 '07 #13
vi******@gmail.com wrote:
On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
>How do I sort an string array using pointers

#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() 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 1) {
qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
for (j = 1; j < argc; j++)
puts(argv[j]);
}
exit(EXIT_SUCCESS);
}
Yes, assuming argv[0] represent the program name, I have yet to see an
environment where this isn't the case. Anybody knows of one?

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Dec 1 '07 #14
Tor Rustad wrote:
vi******@gmail.com wrote:
>On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
>>How do I sort an string array using pointers

#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() 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 1) {
qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
for (j = 1; j < argc; j++)
puts(argv[j]);
}
exit(EXIT_SUCCESS);
}

Yes, assuming argv[0] represent the program name, I have yet to see an
environment where this isn't the case. Anybody knows of one?
Not specifically but I would guess that in environments where the C
program is the only program on the device, it would be meaningless for
it to have a name.

Dec 1 '07 #15
On Sat, 01 Dec 2007 13:03:58 -0500,
CBFalconer <cb********@yahoo.comwrote:
santosh wrote:
>CBFalconer wrote:
>>Friedrich Dominicus wrote:
pa*******@yahoo.com writes:

How do I sort an string array using pointers

Tip: qsort + strcmp

Have a nice day of C programming

Simpler: Make a singly linked list and apply merge sort.

How is this method simpler?

Merge sort code is simple. Input is simple, because you just
extend a linked list by one for each input line. No problem as
long as memory holds out.
[ snip of about 150 lines of code ]

But qsort is a standard function, that you do not have to implement.
Therefore it is simpler to just use that, rather than to implement any
sort algorithm yourself. Using qsort() and strcmp, as suggested before,
is likely to take less than 15 lines of code.

Also, the input is an array, not a linked list, and a good assumption
would be that the array should be sorted in place, or at least that the
output should be a sorted array. Translating it into a linked list,
sort, and then translating back, is by no means simpler than just using
a method that acts directly on the data structure at hand.

Besides that, you do not know how qsort is implemented internally. It's
a bit hard to make judgements on the complexity of the algorithm without
that knowledge.

Martien
--
|
Martien Verbruggen | Useful Statistic: 75% of the people make up
| 3/4 of the population.
|
Dec 1 '07 #16

"santosh" <sa*********@gmail.comwrote in message
news:fi**********@registered.motzarella.org...
Tor Rustad wrote:
>vi******@gmail.com wrote:
<snip>
>>
Yes, assuming argv[0] represent the program name, I have yet to see an
environment where this isn't the case. Anybody knows of one?

Not specifically but I would guess that in environments where the C
program is the only program on the device, it would be meaningless for
it to have a name.
when your program consists of source files which are dynamically loaded and
compiled, there is no meaningful name to pass to main...

Dec 1 '07 #17
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
>
On Sat, 01 Dec 2007 07:32:04 -0500, pete wrote:
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
On Sat, 01 Dec 2007 07:06:31 -0500, pete wrote:
vi******@gmail.com wrote:

On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
How do I sort an string array using pointers

#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() arguments are "pointers to
char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);

The type of your strcmp arguments is (char *const). The strcmp
parameter type is (const char *).

char * can be implicitly converted to const char *.

This way would be more better:

return strcmp(* (const char **) p1, * (const char **) p2);

argv is declared as char *[].
argv[n] (which is what p1 and p2 point to) is a char *.
You shouldn't
access a char * as if it were a const char *.

I'm not saying it's not allowed,
because I'm not sure about that, but I
consider it poor style at least.
However,
strcmp *will* access its arguments as type (const char *).

No, it won't.
It will access its parameters as type (const char *), and
its parameters *are* type const char *, as the result of the implicit
conversion from the arguments (of type char *) to const char *.
I can't parse out which effect you say occurs:
"... as the result of the implicit conversion from the arguments
(of type char *) to const char *."

You can
compare it to this:

Good:

char a = 'x';
int b = a;
const int *c = &b;

Bad:

char a = 'x';
const int *c = (int *) &a;
At least in the bad example, your code acknowledges
that the purpose of the cast is to convert the value
to the type of the object receiving the value.

The whole point of casting a pointer initializer,
is to convert the value
to the type of the object receiving the value.

Recording the type history of the value,
is not the purpose of the cast.

--
pete
Dec 1 '07 #18
On Dec 1, 3:22 pm, "cr88192" <cr88...@hotmail.comwrote:
"santosh" <santosh....@gmail.comwrote in message

news:fi**********@registered.motzarella.org...
Tor Rustad wrote:
vipps...@gmail.com wrote:
<snip>
Yes, assuming argv[0] represent the program name, I have yet to see an
environment where this isn't the case. Anybody knows of one?
Not specifically but I would guess that in environments where the C
program is the only program on the device, it would be meaningless for
it to have a name.

when your program consists of source files which are dynamically loaded and
compiled, there is no meaningful name to pass to main...
argv[0] is always present. C99 draft N1256 (section 5.1.2.2.1):
"If the value of argc is greater than zero, the string pointed to by
argv[0] represents the program name; argv[0][0] shall be the null
character if the program name is not available from the host
environment. If the value of argc is greater than one, the strings
pointed to by argv[1] through argv[argc-1] represent the program
parameters."
Dec 1 '07 #19
Tor Rustad wrote:
>
.... snip ...
>
Yes, assuming argv[0] represent the program name, I have yet to
see an environment where this isn't the case. Anybody knows of one?
Virtually any embedded program.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 2 '07 #20

"Justin Spahr-Summers" <Ju*****************@gmail.comwrote in message
news:8a**********************************@e23g2000 prf.googlegroups.com...
On Dec 1, 3:22 pm, "cr88192" <cr88...@hotmail.comwrote:
>"santosh" <santosh....@gmail.comwrote in message

news:fi**********@registered.motzarella.org...
Tor Rustad wrote:
>vipps...@gmail.com wrote:
<snip>
>Yes, assuming argv[0] represent the program name, I have yet to see an
environment where this isn't the case. Anybody knows of one?
Not specifically but I would guess that in environments where the C
program is the only program on the device, it would be meaningless for
it to have a name.

when your program consists of source files which are dynamically loaded
and
compiled, there is no meaningful name to pass to main...

argv[0] is always present. C99 draft N1256 (section 5.1.2.2.1):
"If the value of argc is greater than zero, the string pointed to by
argv[0] represents the program name; argv[0][0] shall be the null
character if the program name is not available from the host
environment. If the value of argc is greater than one, the strings
pointed to by argv[1] through argv[argc-1] represent the program
parameters."
yes, but is "\0" sensibly a valid program name?...
I will say it is not, and thus the above statements hold...

so, argv[1] to argv[argc-1] represent the program args, but argv[0] is not
necessarily the program name...

Dec 2 '07 #21
CBFalconer wrote:
Tor Rustad wrote:
.... snip ...
>Yes, assuming argv[0] represent the program name, I have yet to
see an environment where this isn't the case. Anybody knows of one?

Virtually any embedded program.
For some reason, I was talking about "hosted environment". :)

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Dec 2 '07 #22
In article <aK*********************@telenor.com>,
Tor Rustad <to********@hotmail.comwrote:
>Yes, assuming argv[0] represent the program name, I have yet to see an
environment where this isn't the case. Anybody knows of one?
Sure. POSIX offers a couple of exec*() calls in which argv[0]
can be completely arbitrary, and that facility is actually used
at times. For example,

$ ps -fe
[...]
nobody 597 451 0 Sep 21 ? 1:43 (pinger)
nobody 539 451 0 Sep 21 ? 0:04 (unlinkd)
roberson 21333 21332 0 Nov 29 ? 0:00 (dns helper)

The last of those is a process forked off by Netscape.
Historically in BSD, writing to argv[0] changed what was reported
by ps, a facility taken advantage of to indicate program status.
--
"There are some ideas so wrong that only a very intelligent person
could believe in them." -- George Orwell
Dec 2 '07 #23
Tor Rustad wrote, On 02/12/07 01:47:
CBFalconer wrote:
>Tor Rustad wrote:
.... snip ...
>>Yes, assuming argv[0] represent the program name, I have yet to
see an environment where this isn't the case. Anybody knows of one?

Virtually any embedded program.

For some reason, I was talking about "hosted environment". :)
Any Unix like system if the process execing your program decides to lie
about the name of the program it is invoking. Look up the Posix exec*
functions to see what I mean.
--
Flash Gordon
Dec 2 '07 #24
On Sat, 01 Dec 2007 17:43:55 -0500, pete wrote:
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
>On Sat, 01 Dec 2007 07:32:04 -0500, pete wrote:
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
On Sat, 01 Dec 2007 07:06:31 -0500, pete wrote:
vi******@gmail.com wrote:

On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
How do I sort an string array using pointers

#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() arguments are "pointers to char", hence the
following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);

The type of your strcmp arguments is (char *const). The strcmp
parameter type is (const char *).

char * can be implicitly converted to const char *.

This way would be more better:

return strcmp(* (const char **) p1, * (const char **) p2);

argv is declared as char *[].
argv[n] (which is what p1 and p2 point to) is a char *. You
shouldn't
access a char * as if it were a const char *.

I'm not saying it's not allowed,
because I'm not sure about that, but I consider it poor style at
least.

However,
strcmp *will* access its arguments as type (const char *).

No, it won't.
It will access its parameters as type (const char *), and its
parameters *are* type const char *, as the result of the implicit
conversion from the arguments (of type char *) to const char *.

I can't parse out which effect you say occurs:
"... as the result of the implicit conversion from the arguments
(of type char *) to const char *."
char *x = "a";
char *y = "b";
strcmp(x, y);

The arguments are x and y. x and y are of type char *. The arguments are
implicitly converted to the type of the parameters as declared in
strcmp's function prototype.
>You can
compare it to this:

Good:

char a = 'x';
int b = a;
const int *c = &b;

Bad:

char a = 'x';
const int *c = (int *) &a;

At least in the bad example, your code acknowledges that the purpose of
the cast is to convert the value to the type of the object receiving the
value.

The whole point of casting a pointer initializer, is to convert the
value
to the type of the object receiving the value.

Recording the type history of the value, is not the purpose of the cast.
But you weren't casting the pointer value argv[n], you were
reinterpreting its bit pattern, by casting a pointer to argv[n]. That's
what the second example does (except for the actual dereference). That's
what you shouldn't do.

static int
cmpstringp(const void *p1, const void *p2) {
return strcmp(* (char * const *) p1, * (char * const *) p2);
}

cmpstringp will be called by qsort as, for example,
cmpstringp(&argv[1], &argv[2]);

argv[n] is of type char *. So p1 and p2 are pointers to a char *. So they
should be dereferenced as pointers to char *. If you want to get a
const char *, you can convert the value _after_ dereferencing.
Dec 2 '07 #25
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
>
On Sat, 01 Dec 2007 17:43:55 -0500, pete wrote:
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
On Sat, 01 Dec 2007 07:32:04 -0500, pete wrote:
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
On Sat, 01 Dec 2007 07:06:31 -0500, pete wrote:
vi******@gmail.com wrote:

On Dec 1, 4:30 am, pateld...@yahoo.com wrote:
How do I sort an string array using pointers

#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() arguments are "pointers to char",
hence the
following cast plus dereference */
return strcmp(* (char * const *) p1,
* (char * const *) p2);

The type of your strcmp arguments is (char *const). The strcmp
parameter type is (const char *).

char * can be implicitly converted to const char *.

This way would be more better:

return strcmp(* (const char **) p1, * (const char **) p2);

argv is declared as char *[].
argv[n] (which is what p1 and p2 point to) is a char *. You
shouldn't
access a char * as if it were a const char *.

I'm not saying it's not allowed,
because I'm not sure about that, but I consider it poor style at
least.

However,
strcmp *will* access its arguments as type (const char *).

No, it won't.
It will access its parameters as type (const char *), and its
parameters *are* type const char *, as the result of the implicit
conversion from the arguments (of type char *) to const char *.
I can't parse out which effect you say occurs:
"... as the result of the implicit conversion from the arguments
(of type char *) to const char *."

char *x = "a";
char *y = "b";
strcmp(x, y);

The arguments are x and y. x and y are of type char *.
The arguments are
implicitly converted to the type of the parameters as declared in
strcmp's function prototype.
You can
compare it to this:

Good:

char a = 'x';
int b = a;
const int *c = &b;

Bad:

char a = 'x';
const int *c = (int *) &a;
At least in the bad example,
your code acknowledges that the purpose of
the cast is to convert the value to the type
of the object receiving the value.

The whole point of casting a pointer initializer, is to convert the
value
to the type of the object receiving the value.

Recording the type history of the value,
is not the purpose of the cast.

But you weren't casting the pointer value argv[n], you were
reinterpreting its bit pattern, by casting a pointer to argv[n].
That's
what the second example does
(except for the actual dereference). That's
what you shouldn't do.

static int
cmpstringp(const void *p1, const void *p2) {
return strcmp(* (char * const *) p1, * (char * const *) p2);
}

cmpstringp will be called by qsort as, for example,
cmpstringp(&argv[1], &argv[2]);

argv[n] is of type char *.
So p1 and p2 are pointers to a char *. So they
should be dereferenced as pointers to char *. If you want to get a
const char *, you can convert the value _after_ dereferencing.
OK.
I'm thinking about it now.
Thank you.

--
pete
Dec 2 '07 #26
pete wrote:
>
CBFalconer wrote:
The only thing affected is the cmp function, and now the void*
parameters make the central block (that manipulates the list,
including sorting) much simpler and a constant library entry. The
only thing needed in the actual cmp function is something like:

int acmp(const void *litem, const void* ritem) {
const whatever ldptr = litem, rdptr = ritem;
It's not "const whatever",
it's always going to be "const nodeptr".

The way that your mergesort and mergelist functions
call the cmp function,
the argument is always going to be a nodeptr value,
and the only way that the cmp function can use that value
is as a nodeptr value.
... code using ldptr and rdptr ...
}

That's the point.
With nodeptr parameters instead of void * parameters,
the equivalent cmp function becomes:

int acmp(const nodeptr litem, const nodeptr ritem) {
... code using litem and ritem ...
}

... which is a simpler way to write it.
--
pete
Dec 3 '07 #27
pete wrote:
>
pete wrote:

CBFalconer wrote:
The only thing affected is the cmp function, and now the void*
parameters make the central block (that manipulates the list,
including sorting) much simpler and a constant library entry. The
only thing needed in the actual cmp function is something like:
>
int acmp(const void *litem, const void* ritem) {
const whatever ldptr = litem, rdptr = ritem;

It's not "const whatever",
it's always going to be "const nodeptr".
... code using ldptr and rdptr ...
}
Because the code using ldptr and rdptr ...,
is always going to be (ldptr -data) and (rdptr -data).

--
pete
Dec 3 '07 #28
pete wrote:
CBFalconer wrote:
>pete wrote:
.... snip ...
>>
>>The various cmp functions are simpler to write when they have
nodeptr parameters, than they are when they have (void *) parameters.

The only thing affected is the cmp function, and now the void*
parameters make the central block (that manipulates the list,
including sorting) much simpler and a constant library entry. The
only thing needed in the actual cmp function is something like:

int acmp(const void *litem, const void* ritem) {
const whatever ldptr = litem, rdptr = ritem;

... code using ldptr and rdptr ...
}

That's the point.
With nodeptr parameters instead of void * parameters,
the equivalent cmp function becomes:

int acmp(const nodeptr litem, const nodeptr ritem) {
... code using litem and ritem ...
}

... which is a simpler way to write it.
You have once more totally ignored the need to keep the node
maneuvering code independant yet linkable. This absolutely
requires the void* parameters.

I suspect I would not like to have to service any fairly
complicated system designed by you. I do not intend to keep on
repeating the same reasons and having them ignored.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Dec 3 '07 #29
CBFalconer wrote:
>
pete wrote:
CBFalconer wrote:
pete wrote:
... snip ...
>
The various cmp functions are simpler to write when they have
nodeptr parameters,
than they are when they have (void *) parameters.

The only thing affected is the cmp function, and now the void*
parameters make the central block (that manipulates the list,
including sorting) much simpler and a constant library entry. The
only thing needed in the actual cmp function is something like:

int acmp(const void *litem, const void* ritem) {
const whatever ldptr = litem, rdptr = ritem;

... code using ldptr and rdptr ...
}
That's the point.
With nodeptr parameters instead of void * parameters,
the equivalent cmp function becomes:

int acmp(const nodeptr litem, const nodeptr ritem) {
... code using litem and ritem ...
}

... which is a simpler way to write it.

You have once more totally ignored the need to keep the node
maneuvering code independant yet linkable. This absolutely
requires the void* parameters.

I suspect I would not like to have to service any fairly
complicated system designed by you. I do not intend to keep on
repeating the same reasons and having them ignored.
I challenge.

You write a C program that sorts a linked list with your
generic list sorting functions.

I'll rewrite it, making only two changes;
1 The parameters on the cmp function will be changed
from void * to nodeptr.
2 The cmp function definition will be simplified.

If the (void *) parameters on the cmp function
are really needed for anything,
then you should be able to come up with
program that I can't convert.

If you won't,
it's because you know that you can't.

--
pete
Dec 4 '07 #30
On Sat, 01 Dec 2007 18:24:11 -0500,
CBFalconer <cb********@yahoo.comwrote:
Martien Verbruggen wrote:
>>
... snip ...
>>
Also, the input is an array, not a linked list, and a good
assumption would be that the array should be sorted in place, or
at least that the output should be a sorted array. Translating it
into a linked list, sort, and then translating back, is by no
means simpler than just using a method that acts directly on the
data structure at hand.

I did NOT recommend converting arrays to lists.
The OP, however, has an array. So they would have to convert, even if
you don't recommend it.

Martien
--
|
Martien Verbruggen | It's not what we don't know that hurts us,
| it's what we know for certain that just ain't
| so. -- Mark Twain
Dec 4 '07 #31
On Sat, 01 Dec 2007 15:37:33 +0530, santosh <sa*********@gmail.com>
wrote:
>aa*****@gmail.com wrote:
>On Nov 30, 9:44 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>pateld...@yahoo.com wrote:
How do I sort an string array using pointers

How do you think? Post an attempt and you will get help.

--
Ian Collins.

what about this attempt
snip abominable code
>Please do submit this to:

<http://ioccc.org/>
Since it doesn't compile, why bother?
Remove del for email
Dec 4 '07 #32
pete wrote:
>
.... snip ...
>
I challenge.

You write a C program that sorts a linked list with your
generic list sorting functions.

I'll rewrite it, making only two changes;
1 The parameters on the cmp function will be changed
from void * to nodeptr.
2 The cmp function definition will be simplified.

If the (void *) parameters on the cmp function are really needed
for anything, then you should be able to come up with program
that I can't convert.

If you won't, it's because you know that you can't.
The problem is not just the one program. It is the one or ten
dozen further programs that use the same list and sorting module,
unchanged, and which programs do all sorts of different things.
Not only do you want to write portable code, you want to write
reusable portable code.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 4 '07 #33
CBFalconer wrote:
>
pete wrote:
... snip ...

I challenge.

You write a C program that sorts a linked list with your
generic list sorting functions.

I'll rewrite it, making only two changes;
1 The parameters on the cmp function will be changed
from void * to nodeptr.
2 The cmp function definition will be simplified.

If the (void *) parameters on the cmp function are really needed
for anything, then you should be able to come up with program
that I can't convert.

If you won't, it's because you know that you can't.

The problem is not just the one program. It is the one or ten
dozen further programs that use the same list and sorting module,
unchanged, and which programs do all sorts of different things.
I've got three of them right here:
http://www.mindspring.com/~pfilandr/.../string_sort.c
http://www.mindspring.com/~pfilandr/...es/file_sort.c
http://www.mindspring.com/~pfilandr/...s/file_parse.c
Not only do you want to write portable code, you want to write
reusable portable code.
The associated files are here:
http://www.mindspring.com/~pfilandr/...les/list_lib.h
http://www.mindspring.com/~pfilandr/...les/file_lib.h
http://www.mindspring.com/~pfilandr/...les/list_lib.c
http://www.mindspring.com/~pfilandr/...les/file_lib.c

--
pete
Dec 4 '07 #34

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.