Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   C Pointers (http://www.velocityreviews.com/forums/t556466-c-pointers.html)

pateldm15@yahoo.com 12-01-2007 02:30 AM

C Pointers
 
How do I sort an string array using pointers

Ian Collins 12-01-2007 02:44 AM

Re: C Pointers
 
pateldm15@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.

erfan 12-01-2007 07:21 AM

Re: C Pointers
 
On 12月1日, 下午4时07分, Friedrich Dominicus <just-for-news-fr...@q-software-
solutions.de> wrote:
> 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:)

CBFalconer 12-01-2007 07:50 AM

Re: C Pointers
 
Friedrich Dominicus wrote:
> pateldm15@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


Friedrich Dominicus 12-01-2007 08:07 AM

Re: C Pointers
 
pateldm15@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.

santosh 12-01-2007 09:04 AM

Re: C Pointers
 
CBFalconer wrote:

> Friedrich Dominicus wrote:
>> pateldm15@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?


aarklon@gmail.com 12-01-2007 09:48 AM

Re: C Pointers
 
On Nov 30, 9:44 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> 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++;
}
}
}

santosh 12-01-2007 10:07 AM

Re: C Pointers
 
aarklon@gmail.com wrote:

> On Nov 30, 9:44 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> 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/>


pete 12-01-2007 10:41 AM

Re: C Pointers
 
pateldm15@yahoo.com 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

vippstar@gmail.com 12-01-2007 11:21 AM

Re: C Pointers
 
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);
}


All times are GMT. The time now is 04:48 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.