# 这样返回指针为什么只能输出一个元素解决方案

www.MyException.Cn  网友分享于：2013-02-14  浏览：3次

#include   <stdio.h>
int *judge   (char   ch[100],int   i);
main   ()
{
char ch[100];
int *p,i=0,j=0;
scanf( "%s ",ch);
while   (ch[j++])   i++;
p=judge(ch,i);
printf( "Big   letter:%d\n ",*p);
printf( "Small   letter:%d\n ",*(p+1));
printf( "Space:%d\n ",*(p+2));
printf( "Number:%d\n ",*(p+3));
printf( "Others:%d ",*(p+4));
getch();

}

int *judge   (char   ch[100],int   i)
{
int a[5]={0,0,0,0,0},j,*p;
for   (j=0;j <i;j++)
{
if   (ch[j]> = 'A '   &&   ch[j] <= 'Z ')
a[0]++;
else   if   (ch[j]> = 'a '   &&   ch[j] <= 'z ')
a[1]++;
else   if   (ch[j]== '   ')
a[2]++;
else   if   (ch[j]> = '0 '   &&   ch[j] <= '9 ')
a[3]++;
else   a[4]++;
}
p=a;
return   p;
}

------解决方案--------------------

------解决方案--------------------
int *judge (char ch[100],int i)
{
int a[5]={0,0,0,0,0},j,*p;
for (j=0;j <i;j++)
{
if (ch[j]> = 'A ' && ch[j] <= 'Z ')
a[0]++;
else if (ch[j]> = 'a ' && ch[j] <= 'z ')
a[1]++;
else if (ch[j]== ' ')
a[2]++;
else if (ch[j]> = '0 ' && ch[j] <= '9 ')
a[3]++;
else a[4]++;
}
p=(int *)malloc(sizeof(int)*5);
memset(p,0,sizeof(int)*5);
memcpy(p,&a,sizeof(int)*5);
return p;
}

------解决方案--------------------

------解决方案--------------------