派大星星️ 2021-10-15 09:00 采纳率: 100%
浏览 27
已结题

数组和指针不是可以相互转化吗?

#include <stdio.h>
struct stu
{
int number;
double *score;
};
int main()
{
struct stu student[4];
scanf("%lf,",&student[0].score[0]);
printf("%f",student[0].score[0]);
return 0;
}

输出根本没反应😭😭😭

  • 写回答

2条回答 默认 最新

  • qfl_sdu 2021-10-15 09:37
    关注

    struct stu student[4];这里声明了数组,但是数组元素的score没有初始化,scanf("%lf,",&student[0].score[0]);这里使用的时候程序会崩的。
    参考如下:

    #include <stdio.h>
    struct stu
    {
        int number;
        double *score;
    };
    int main()
    {
        struct stu student[4];
        double arr[3];
        student[0].score = arr; //这里是你想要的指针和数组的转换,但是一般不这么用,会破坏结构体的封装性
        scanf("%lf,",&student[0].score[0]);
        printf("%f",student[0].score[0]);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月23日
  • 已采纳回答 10月15日
  • 创建了问题 10月15日

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?