Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C Pointers

Reply
Thread Tools

C Pointers

 
 
pateldm15@yahoo.com
Guest
Posts: n/a
 
      12-01-2007
How do I sort an string array using pointers
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-01-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> How do I sort an string array using pointers


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

--
Ian Collins.
 
Reply With Quote
 
 
 
 
erfan
Guest
Posts: n/a
 
      12-01-2007
On 12月1日, 下午4时07分, Friedrich Dominicus <just-for-news-fr...@q-software-
solutions.de> wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-01-2007
Friedrich Dominicus wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
Friedrich Dominicus
Guest
Posts: n/a
 
      12-01-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      12-01-2007
CBFalconer wrote:

> Friedrich Dominicus wrote:
>> (E-Mail Removed) 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?

 
Reply With Quote
 
aarklon@gmail.com
Guest
Posts: n/a
 
      12-01-2007
On Nov 30, 9:44 pm, Ian Collins <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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++;
}
}
}
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      12-01-2007
(E-Mail Removed) wrote:

> On Nov 30, 9:44 pm, Ian Collins <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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/>

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-01-2007
(E-Mail Removed) wrote:
>
> How do I sort an string array using pointers


/* BEGIN pt_sort.c */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SORT_FUNCTIONS { \
no_sort, "Original order of the test arrays:", \
i_sort, "Stable insertionsort:", \
slsort, "Nonstable simple selection sort;\nfour is " \
"after nine, and three is after eight:", \
qsort, "Standard library qsort;\n" \
"unspecified ordering of equal keys:" \
}
#define NUMBERS { \
"one", "two","three","four","five", \
"six","seven","eight","nine", "ten" \
}
#define NMEMB (sizeof numbers / sizeof *numbers)
#define SORTS (sizeof s_F / sizeof *s_F)

int lencomp(const void *a, const void *b);
void no_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
void i_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
void slsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

int main(void)
{
size_t element, sort;
struct sf {
void (*function)(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
const char *description;
} s_F[] = SORT_FUNCTIONS;
const char *numbers[] = NUMBERS;
char *copy[NMEMB];

puts("\n/* BEGIN output from pt_sort.c */\n\n"
"Arrays of type char *,\n"
"are being sorted by string length.\n");
for (sort = 0; sort != SORTS; ++sort) {
puts(s_F[sort].description);
memcpy(copy, numbers, sizeof copy);
s_F[sort].function(copy, NMEMB, sizeof *copy, lencomp);
for (element = 0; element != NMEMB; ++element) {
puts(copy[element]);
}
putchar('\n');
}
puts("/* END output from pt_sort.c */");
return 0;
}

int lencomp(const void *a, const void *b)
{
const size_t a_len = strlen(*(const char **)a);
const size_t b_len = strlen(*(const char **)b);

return b_len > a_len ? -1 : b_len != a_len;
}

void no_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
base, nmemb, size, compar;
}

void i_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
unsigned char *array, *high, *low, *p1, *p2, *end, swap;

if (nmemb-- > 1) {
array = base;
do {
low = array;
array += size;
high = array;
while (compar(low, high) > 0) {
p1 = low;
p2 = high;
end = p2 + size;
do {
swap = *p1;
*p1++ = *p2;
*p2++ = swap;
} while (p2 != end);
if (low == base) {
break;
}
high = low;
low -= size;
}
} while (--nmemb != 0);
}
}

void slsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
size_t tail;
unsigned char *array, *first, *middle, *p1, *p2, *end, swap;

for (array = base; nmemb-- > 1; array += size) {
middle = first = array + size;
tail = nmemb;
while (--tail != 0) {
middle += size;
if (compar(first, middle) > 0) {
first = middle;
}
}
if (compar(array, first) > 0) {
p1 = array;
p2 = first;
end = p2 + size;
do {
swap = *p1;
*p1++ = *p2;
*p2++ = swap;
} while (p2 != end);
}
}
}

/* END pt_sort.c */


--
pete
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      12-01-2007
On Dec 1, 4:30 am, (E-Mail 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);
}

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);
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Does deleting a container of pointers also delete the (contained) pointers? Xamalek C++ 7 11-04-2003 04:17 PM
c++: pointers to pointers A C++ 3 10-29-2003 01:15 PM
pointers to pointers // exception handling error muser C++ 3 09-18-2003 06:19 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments