是否有更简单的方法来使用指针(包括示例代码) [英] Is there an easier way to work with pointers (sample code included)
问题描述
是否有一种更简单的方法来编写cmp程序而不必通过所有指针操作?
#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;
返回strcmp(* s1,* s2);
}
int main(无效)
{
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(" \ base [1]%s",base [1]);
printf(" \ base [2]%s \ n",base [2]);
qsort(& base,3,sizeof(base [0]),(void *)cmp);
printf(" \\\
base [0 ]%s",base [0]);
printf(" \ base [1]%s",base [1]);
printf(" ; \ nbase [2]%s \ n",base [2]);
}
TIA,
Walter
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
推荐答案
2006年3月8日10:22:53 -0800, wa *************** @ gmail.com 写道:
On 8 Mar 2006 10:22:53 -0800, wa***************@gmail.com wrote:
有没有一种更简单的方法来编写cmp程序而不必通过所有指针操作?
#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;
返回strcmp(* s1,* s2);
}
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;
返回strcmp(s1,s2);
}
这就是你要做的事情吗?
BTW,请妥善缩进发布的代码。您的编译器可能不在乎,
但是人们在阅读它。
-
Al Balmer
Sun City,AZ
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
wa***************@gmail.com 写道:
是否有一种更简单的方法来编写cmp程序而不必通过所有
除了过多无用的演员阵容外,你还遗漏了一个
的关键标题。
是的,你是太努力了。将下面的代码与您的代码进行比较,
跟随它:
#include< stdio.h>
#include < stdlib.h>
#include< string.h>
int compare_strings(const void * i,const void * j)
{
返回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(无效)
{
char * base [] = {" zebra"," ;你好,再见 };
size_t n = sizeof base / sizeof * base;
showstrs(n,base);
putchar(''\ n '');
qsort(base,n,sizeof * base,compare_strings);
showstrs(n,base);
返回0 ;
}
[输出]
s [0]斑马
s [1]你好< br $>
s [2]再见
s [0]再见
s [1]你好
s [2] zebra
[OP'代码]
#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;
返回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;
ba se [2] =(char *)string2;
printf(" \ base [0]%s",base [0]);
printf(" \ nbase) [1]%s",base [1]);
printf(" \ base [2]%s \ n",base [2]);
qsort( & base,3,sizeof(base [0]),(void *)cmp);
printf(" \ base [0]%s",base [0]);
printf(" \ base [1]%s",base [1]);
printf(" \ base [2]%s \ n",base [2]);
}
TIA,
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?
文章< 11 ********************** @ z34g2000cwc.googlegroups .com>,
< wa *************** @ gmail.com>写道:
In article <11**********************@z34g2000cwc.googlegroups .com>,
<wa***************@gmail.com> wrote:
有没有一种更简单的方法来编写cmp程序而不必通过所有指针操作?
#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;
返回strcmp(* s1,* s2);
}
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);
}
[...其余代码被剪断。 ..]
通常,在代码中使用强制转换表示
出错了。这条规则有例外,但在
一般情况下,演员应该在你的脑海中引起一个红旗。这是
代码的修改版本,没有强制转换。它是短的
,应该更容易理解和调试:
#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;
返回strcmp(* aa,* bb);
}
int main(无效)
{
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]);
返回EXIT_SUCCESS;
}
-
Rouben Rostamian
[... 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
这篇关于是否有更简单的方法来使用指针(包括示例代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!