关于字符指针数组问题
#include <stdio.h>
#include <string.h>
int geshu();
void shuru(char *[], int);
void paixu(char *[], int);
void main()
{
char * no[10];
int z;
z = geshu();
shuru(no,z);
paixu(no,z);
}
void paixu(char * nn[10],int i)
{
int x, y, j;
char * temp;
for (x = 0;x < i;x++)
{
for (y = 0;y<i-x-1;y++)
{
j=strcmp(nn[y],nn[y+1]);//为什么改成(*nn[y],*nn[y+1])会错呢?
//nn是字符指针数组,放的是地址。strcmp(,);比的是字符串,因该是
//取地址的值才对撒。
if (j == 1)
{
temp = nn[y];
nn[y] = nn[y+1];
nn[y+1] = temp;
}
}
}
}
void shuru(char * z[10], int i)
{
int x;
for (x = 0; x < i; x++)
{
fflush(stdin);
gets(z[x]);
fflush(stdin);
}
}
int geshu()
{
int x;
printf("请输入多少直");
scanf("%d", &x);
return x;
}
问题点数:20、回复次数:4Top
1 楼tailzhou(尾巴)回复于 2006-07-02 23:01:50 得分 20
char * no[10];
没给 no数组分配内存
在shuru函数内的gets(z[x]);语句前面
z[x]=malloc(100); //100改为你的输入的最大字符串的长度
Top
2 楼kuvske()回复于 2006-07-02 23:06:41 得分 0
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int geshu();
void shuru(char *[], int);
void paixu(char *[], int);
void main()
{
char * no[10];
int z;
z = geshu();
shuru(no,z);
paixu(no,z);
}
void paixu(char * nn[10],int i)
{
int x, y, j;
char * temp;
for (x = 0;x < i;x++)
{
for (y = 0;y<i-x-1;y++)
{
j=strcmp(nn[y],nn[y+1]);//为什么改成(*nn[y],*nn[y+1])会错呢?
//nn是字符指针数组,放的是地址。strcmp(,);比的是字符串,因该是
//取地址的值才对撒。
if (j == 1)
{
temp = nn[y];
nn[y] = nn[y+1];
nn[y+1] = temp;
}
}
}
}
void shuru(char * z[10], int i)
{
int x;
for (x = 0; x < i; x++)
{
z[x] = (char *)malloc(15);
fflush(stdin);
gets(z[x]);
fflush(stdin);
}
}
int geshu()
{
int x;
printf("请输入多少直");
scanf("%d", &x);
return x;
}这不是问题关键Top
3 楼kuvske()回复于 2006-07-02 23:07:48 得分 0
j=strcmp(nn[y],nn[y+1]);//为什么改成(*nn[y],*nn[y+1])会错呢?
//nn是字符指针数组,放的是地址。strcmp(,);比的是字符串,因该是
//取地址的值才对撒。
Top
4 楼littlefish1981()回复于 2006-07-03 10:32:57 得分 0
lz对strcmp函数理解有误,查查msdn就知道了。
int strcmp(const char *string1,const char *string2)
函数接收的参数就是char *型的,代表字符串,而*(char *)仅仅是一个字符。Top




