C言語はポインタ配列を用いて入力された3つの文字列を小さい順に出力する
1048 ワード
ポインタ配列を定義するとこの問題を処理しやすくなります.
1つ目の書き方:
2つ目はstrcpyを使用して両者の文字列の内容を交換する.
エラーポイント:ブロガーはポインタ変数を交換してポインタの指向を変更して値を変更しようとしたが、C言語の関数は一方向に転送され、関数ブロックのポインタ変数は指向を変更しなかった.ここに記録しておきます.
1つ目の書き方:
#include
#include
#include
#include // sin x,cos x,e^x
//
int main()
{
char str1[10],str2[10],str3[10];
char *p[3];
int i;
printf("
");
gets(str1);
gets(str2);
gets(str3);
//
p[0]=str1;
p[1]=str2;
p[2]=str3;
// , ,
swap(p);
//
for (i=0;i<3;i++)
{
printf("%s
",p[i]);
}
//
}
swap(char *p[])
{
//
char *temp;
int i,j;
for (i=0;i<3;i++)
{
//printf("%s
",p[i]);
for (j=i+1;j<3;j++)
{
if (strcmp(p[i],p[j])>0)//
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
}
2つ目はstrcpyを使用して両者の文字列の内容を交換する.
エラーポイント:ブロガーはポインタ変数を交換してポインタの指向を変更して値を変更しようとしたが、C言語の関数は一方向に転送され、関数ブロックのポインタ変数は指向を変更しなかった.ここに記録しておきます.