高手来 C语言

来源:百度知道 编辑:UC知道 时间:2024/06/01 11:02:39
#include<stdio.h>
#include<string.h>
struct student
{char name[20];
int age;
int phones;
};
struct student stu[3]={{"yangjianlin",28,112},
{"yangjianwei",22,110},
{"yangchao",21,1113}};
main( )
{struct student *p;
p=stu;
char b[20];
int i;
printf("name=");
scanf("%s",b);
for(i=0;i<3;i++)
if(strcmp((p+i)->name,b)==0)
printf("name:%s\nage:%d\nphones number:%d",(p+i)->name,(p+i)->age,(p+i)->phones);
}改正下啊

#include<stdio.h>
#include<string.h>
struct student
{
char name[20];
int age;
int phones;
};
struct student stu[3]={{"yangjianlin",28,112},
{"yangjianwei",22,110},
{"yangchao",21,1113}};
int main( )
{
struct student *p;
p=stu;
char b[20];
int i;
printf("name=");
scanf("%s",b);
for(i=0;i<3;i++)
{
if(strcmp((p+i)->name,b)==0)
printf("name:%s\nage:%d\nphones number:%d\n",(p+i)->name,(p+i)->age,(p+i)->phones);
else
printf("查无此人\n");
}
}

char b[20];
int i;
放到
p=stu;
前面,内存泄露,标准c不允许
放前面就不会出错了

#include<stdio.h>
#include<string.h>
struct student
{char name[20];
int age;
int phones;
};
struct student stu[3]=
{
{&