Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is there an easier way to work with pointers (sample code included)

Reply
Thread Tools

Is there an easier way to work with pointers (sample code included)

 
 
walter.preuninger@gmail.com
Guest
Posts: n/a
 
      03-08-2006
Is there an easier way to code the cmp procedure without going thru all
the pointer manipulations?

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

int cmp(const void *i, const void *j)
{
void *p1, *p2;
char **s1, **s2;
p1=(void *) i;
p2=(void *) j;
s1=(void *) p1;
s2=(void *) p2;
return strcmp(*s1,*s2);
}

int main(void)
{
char string0[]="zebra";
char string1[]="hello";
char string2[]="goodbye";

char *base[3];

base[0]=(char *)string0;
base[1]=(char *)string1;
base[2]=(char *)string2;

printf("\nbase[0] %s",base[0]);
printf("\nbase[1] %s",base[1]);
printf("\nbase[2] %s\n",base[2]);

qsort(&base,3,sizeof(base[0]),(void *)cmp);

printf("\nbase[0] %s",base[0]);
printf("\nbase[1] %s",base[1]);
printf("\nbase[2] %s\n",base[2]);
}


TIA,

Walter

 
Reply With Quote
 
 
 
 
Al Balmer
Guest
Posts: n/a
 
      03-08-2006
On 8 Mar 2006 10:22:53 -0800, wrote:

>Is there an easier way to code the cmp procedure without going thru all
>the pointer manipulations?
>
>#include <stdlib.h>
>#include <string.h>
>
>int cmp(const void *i, const void *j)
>{
>void *p1, *p2;
>char **s1, **s2;
>p1=(void *) i;
>p2=(void *) j;
>s1=(void *) p1;
>s2=(void *) p2;
>return strcmp(*s1,*s2);
>}


int cmp(const void *i, const void *j)
{
char *s1 = i;
char *s2 = p;

return strcmp(s1, s2);
}
Is that what you're trying to do?

BTW, please properly indent code you post. Your compiler may not care,
but people reading it do.

--
Al Balmer
Sun City, AZ
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-08-2006
wrote:
> Is there an easier way to code the cmp procedure without going thru all
> the pointer manipulations?


In addition to an excessive number of useless casts, you have left out a
key header.
Yes, you are trying too hard. Compare the code below to your code,
which follows it:

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

int compare_strings(const void *i, const void *j)
{
return strcmp(*(char **) i, *(char **) j);
}

void showstrs(size_t n, char *s[n])
{
size_t i;
for (i = 0; i < n; i++)
printf("s[%u] %s\n", (unsigned) i, s[i]);
}

int main(void)
{
char *base[] = { "zebra", "hello", "goodbye" };
size_t n = sizeof base / sizeof *base;
showstrs(n, base);
putchar('\n');
qsort(base, n, sizeof *base, compare_strings);
showstrs(n, base);
return 0;
}

[output]
s[0] zebra
s[1] hello
s[2] goodbye

s[0] goodbye
s[1] hello
s[2] zebra


[OP's code]

> #include <stdlib.h>
> #include <string.h>
>
> int cmp(const void *i, const void *j)
> {
> void *p1, *p2;
> char **s1, **s2;
> p1=(void *) i;
> p2=(void *) j;
> s1=(void *) p1;
> s2=(void *) p2;
> return strcmp(*s1,*s2);
> }
>
> int main(void)
> {
> char string0[]="zebra";
> char string1[]="hello";
> char string2[]="goodbye";
>
> char *base[3];
>
> base[0]=(char *)string0;
> base[1]=(char *)string1;
> base[2]=(char *)string2;
>
> printf("\nbase[0] %s",base[0]);
> printf("\nbase[1] %s",base[1]);
> printf("\nbase[2] %s\n",base[2]);
>
> qsort(&base,3,sizeof(base[0]),(void *)cmp);
>
> printf("\nbase[0] %s",base[0]);
> printf("\nbase[1] %s",base[1]);
> printf("\nbase[2] %s\n",base[2]);
> }
>
>
> TIA,

^^^
Are you a bill-collector or a script-kiddie?

 
Reply With Quote
 
Rouben Rostamian
Guest
Posts: n/a
 
      03-08-2006
In article < .com>,
<> wrote:
>Is there an easier way to code the cmp procedure without going thru all
>the pointer manipulations?
>
>#include <stdlib.h>
>#include <string.h>
>
>int cmp(const void *i, const void *j)
>{
>void *p1, *p2;
>char **s1, **s2;
>p1=(void *) i;
>p2=(void *) j;
>s1=(void *) p1;
>s2=(void *) p2;
>return strcmp(*s1,*s2);
>}

[... the rest of the code snipped ...]

As a rule, the use of casts in a code is an indication that
something is wrong. There are exceptions to this rule, but in
general a cast should raise a red flag in your mind. Here is
a modified version of your code without casts. It is shorte
and should be easier to understand and debug:

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

int cmp(const void *a, const void *b)
{
char * const *aa = a;
char * const *bb = b;
return strcmp(*aa, *bb);
}

int main(void)
{
char string0[] = "zebra";
char string1[] = "hello";
char string2[] = "goodbye";
char *base[3];

base[0] = string0;
base[1] = string1;
base[2] = string2;

qsort(base, (sizeof base)/(sizeof base[0]), sizeof base[0], cmp);

printf("base[0] = %s\n",base[0]);
printf("base[1] = %s\n",base[1]);
printf("base[2] = %s\n",base[2]);

return EXIT_SUCCESS;
}

--
Rouben Rostamian
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-08-2006
Al Balmer wrote:
> On 8 Mar 2006 10:22:53 -0800, wrote:
>
>> Is there an easier way to code the cmp procedure without going thru all
>> the pointer manipulations?
>>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> int cmp(const void *i, const void *j)
>> {
>> void *p1, *p2;
>> char **s1, **s2;
>> p1=(void *) i;
>> p2=(void *) j;
>> s1=(void *) p1;
>> s2=(void *) p2;
>> return strcmp(*s1,*s2);
>> }

>
> int cmp(const void *i, const void *j)
> {
> char *s1 = i;
> char *s2 = p;


Don't you mean:
const char *s1 = i;
const char *s2 = j;

> return strcmp(s1, s2);
> }
> Is that what you're trying to do?
>
> BTW, please properly indent code you post. Your compiler may not care,
> but people reading it do.


Definitely.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      03-08-2006
Al Balmer wrote:
[...qsort compare function on string array...]
> int cmp(const void *i, const void *j)
> {
> char *s1 = i;
> char *s2 = p;
>
> return strcmp(s1, s2);
> }

[...]

Any reason you can't simply do:

int cmp(const void *i, const void *j)
{
return strcmp(i,j);
}

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <private.php?do=newpm&u=>

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      03-08-2006



<> wrote in message
news: oups.com...
> Is there an easier way to code the cmp procedure without going thru all
> the pointer manipulations?
>
> #include <stdlib.h>
> #include <string.h>
>
> int cmp(const void *i, const void *j)
> {
> void *p1, *p2;
> char **s1, **s2;
> p1=(void *) i;
> p2=(void *) j;
> s1=(void *) p1;
> s2=(void *) p2;
> return strcmp(*s1,*s2);
> }
>
> int main(void)
> {
> char string0[]="zebra";
> char string1[]="hello";
> char string2[]="goodbye";
>
> char *base[3];
>
> base[0]=(char *)string0;
> base[1]=(char *)string1;
> base[2]=(char *)string2;
>
> printf("\nbase[0] %s",base[0]);
> printf("\nbase[1] %s",base[1]);
> printf("\nbase[2] %s\n",base[2]);
>
> qsort(&base,3,sizeof(base[0]),(void *)cmp);
>
> printf("\nbase[0] %s",base[0]);
> printf("\nbase[1] %s",base[1]);
> printf("\nbase[2] %s\n",base[2]);
> }
>
>
> TIA,
>
> Walter
>


Sorting a plain array of strings is usually pretty pointless.
Do something like this

typedef struct
{
char name[64];
char *description;
/* probably lots of other data associated with animals here */
} ANIMAL;

int cmp(const void *e1, const void *e2)
{
const ANIMAL *a1 = e1;
const ANIMAL *a2 = e2;

return strcmp(al->name, a2->name);
}

That's not too burdensome.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $6.90 paper, available www.lulu.com


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      03-08-2006
wrote:

> Is there an easier way to code the cmp procedure without going thru
> all the pointer manipulations?
>
> #include <stdlib.h>
> #include <string.h>
>
> int cmp(const void *i, const void *j)
> {
> void *p1, *p2;
> char **s1, **s2;
> p1=(void *) i;
> p2=(void *) j;
> s1=(void *) p1;
> s2=(void *) p2;
> return strcmp(*s1,*s2);
> }



In addition to what all the others have said, you should almost NEVER
cast away const from a pointer. That's a major indicator that there's a
design flaw.



Brian
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-08-2006
"Default User" <> writes:

> wrote:
>
> > Is there an easier way to code the cmp procedure without going thru
> > all the pointer manipulations?
> >
> > #include <stdlib.h>
> > #include <string.h>
> >
> > int cmp(const void *i, const void *j)
> > {
> > void *p1, *p2;
> > char **s1, **s2;
> > p1=(void *) i;
> > p2=(void *) j;
> > s1=(void *) p1;
> > s2=(void *) p2;
> > return strcmp(*s1,*s2);
> > }

>
>
> In addition to what all the others have said, you should almost NEVER
> cast away const from a pointer. That's a major indicator that there's a
> design flaw.


The exception would be when you're implementing something like
strchr(); where you want your parameter list to indicate a guarantee
that you won't change the string, but you don't want to force the
caller to make that same guarantee (or do the cast there).
 
Reply With Quote
 
Pierre Maurette
Guest
Posts: n/a
 
      03-09-2006
, le 08/03/2006 a écrit :
> Is there an easier way to code the cmp procedure without going thru all
> the pointer manipulations?


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

#define AFFICHE printf("\nbase[0] %s""\nbase[1] %s""\nbase[2] %s\n",\
base[0],\
base[1],\
base[2])

typedef char* chaine;
typedef int(*f_comp_t)(const void*, const void*);


int cmp_chaine(const chaine* i, const chaine* j)
{
return strcmp(*i,*j);
}

int main(void)
{
char string0[]="zebra", string1[]="hello", string2[]="goodbye";
chaine base[] = {string0, string1, string2};
AFFICHE;
qsort(base,3,sizeof(chaine),(f_comp_t)cmp_chaine);
AFFICHE;
return 0;
}

Or:

/* .... */

int main(void)
{
char string0[]="zebra", string1[]="hello", string2[]="goodbye";
chaine base[] = {string0, string1, string2};
f_comp_t cmp = (f_comp_t)cmp_chaine;
AFFICHE;
qsort(base,3,sizeof(chaine),cmp);
AFFICHE;
return 0;
}

--
Pierre Maurette


 
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
There must be an easier way Jim S HTML 3 02-15-2007 02:39 AM
Is there an easier way to express this list slicing? John Henry Python 19 12-01-2006 08:30 AM
Hash of Structs in a Package, is there an easier way? Norman Ackroyd Perl 1 07-28-2004 11:54 AM
Is there an easier way to break a list into sub groups MetalOne Python 4 02-22-2004 09:58 PM
Defrag XP There must be an easier way. ChrisTOE@address.invalid Computer Support 4 01-19-2004 03:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57