P
pateldm15
How do I sort an string array using pointers
How do I sort an string array using pointers
Tip: qsort + strcmp
Have a nice day of C programming
Friedrich
Friedrich said:Tip: qsort + strcmp
Have a nice day of C programming
Tip: qsort + strcmpHow do I sort an string array using pointers
CBFalconer said:Simpler: Make a singly linked list and apply merge sort.
How do you think? Post an attempt and you will get help.
How do you think? Post an attempt and you will get help.
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++;
}
}
}
How do I sort an string array using pointers
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);
}
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);
=?UTF-8?q?Harald_van_D=C4=B3k?= said: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.
=?UTF-8?q?Harald_van_D=C4=B3k?= said:(e-mail address removed) wrote:
On Dec 1, 4:30 am, (e-mail address removed) 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 *).
santosh said:How is this method simpler?
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);
}
Tor said: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?
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.
santosh said:Tor said:(e-mail address removed) wrote: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.
=?UTF-8?q?Harald_van_D=C4=B3k?= said:=?UTF-8?q?Harald_van_D=C4=B3k?= said:On Dec 1, 4:30 am, (e-mail address removed) 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;
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.