99 date:2021.2.17
要点:
结构变量各个成员的表示方法:
*(结构指针变量).成员名;或 结构指针变量 ->成员名
详细代码如下:
#include <stdio.h>
#include <string.h>
struct student {
long sno;
char name[10];
float score[3];
};
/**********found**********/
struct student* fun(struct student *a) //结构指针变量
{ int i;
a->sno = 10002;
strcpy(a->name, "LiSi");
/**********found**********/
for (i=0; i<3; i++) a->score[i] += 1;
/**********found**********/
return a ;
}
void main()
{ struct student s={10001,"ZhangSan", 95, 80, 88}, *t;
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);
for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);
printf("\n");
t = fun(&s);
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",t->sno, t->name);
for (i=0; i<3; i++) printf("%6.2f ", t->score[i]);
printf("\n");
}
要点: 字符串指针数组
注意输入格式-换行 \n
详细代码如下:
#include <stdio.h>
#include <string.h>
#define N 5
#define M 81
/**********found**********/
char * fun(char (*sq)[M]) //字符串指针数组 根据返回值类型可知应定义为字符型指针函数
{ int i; char *sp;
sp=sq[0];
for(i=0;i<N;i++)
if(strlen( sp)<strlen(sq[i]))
sp=sq[i] ;
/**********found**********/
return sp;
}
void main()
{ char str[N][M], *longest; int i;
printf("Enter %d lines :\n",N);
for(i=0; i<N; i++) gets(str[i]);
printf("\nThe %d string :\n",N);
for(i=0; i<N; i++) puts(str[i]);
longest=fun(str);
printf("\nThe longest string :\n"); puts(longest);
}