为什么scanf中 s2.name前面不用&

int main()
{
    struct stu s1 = { "zhangsan",20,95.6f };
    struct stu s2 = { "lisi",40,99.8f };
    //结构体变量.结构体成员
    //.
    //->
    //输入数据到s2中
    scanf("%s %d %f", s2.name, &(s2.age), &(s2.score));    //s2.name前面不用&

    printf("%s %d %.2f\n", s2.name, s2.age, s2.score);


    return 0;

}

因为name是数组,数组本来就是地址,所以不需要取地址运算符。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请修改以下代码,使其能够正常运行。#include<stdio.h> #include<string.h> #include<stdlib.h> struct date { int year; int month; int day; }; struct stu { int num; char name[20]; char sex; struct date birth; float score; }; int main() { struct stu s1 = {10010,"zhangsan",'m',2000,5,4,84.5}, s2, s3; /*****输入学生信息存放在变量s2*****/ /********** Begin **********/ scanf("%d",&(s2.num)); scanf("%s",&(s2.name)); scanf("%c",&(s2.sex)); scanf("%d %d %d",&(s2.birth.year),&(s2.birth.month),&(s2.birth.day)); scanf("%f",&(s2.score)); /********** End **********/ /*****交换两个结构体变量s1和s2*****/ /********** Begin **********/ strcpy(s1.num,s3.num); strcpy(s1.name,s3.name); strcpy(s1.sex,s3.sex); strcpy(s1.date.birth,s3.date.birth); strcpy(s1.score,s3.score); strcpy(s1.num,s2.num); strcpy(s1.name,s2.name); strcpy(s1.sex,s2.sex); strcpy(s1.date.birth,s2.date.birth); strcpy(s1.score,s2.score); strcpy(s3.num,s2.num); strcpy(s3.name,s2.name); strcpy(s3.sex,s2.sex); strcpy(s3..birth,s2.birth); strcpy(s3.score,s2.score); /********** End **********/ /*****输出结构体变量s1和s2的成员*****/ /********** Begin **********/ printf("学号:%d",s1.num); printf("姓名:%s"s1.name); printf("性别:%c",s1.sex); printf("出生日期:%d年%d月%d日",s1.birth.year); printf("成绩:%d",s1.score); printf("学号:%d",s2.num); printf("姓名:%s",s2.name); printf("性别:%d",s2.sex); printf("出生日期:%d年%d月%d日",s2.birth.year,s2.birth.month,s2.birth.day); printf("成绩:%d",s2.score); /********** End **********/ return 0; }
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值