| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 461 人关注过本帖
标题:结构体中,输入输出错 . 和 -> 都不对,错在哪里了 新手求助
只看楼主 加入收藏
xjxjxj
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2022-7-6
结帖率:100%
收藏
已结贴  问题点数:6 回复次数:2 
结构体中,输入输出错 . 和 -> 都不对,错在哪里了 新手求助
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct Student
{
    int age;
    float score;
    char name[100];
};

int main(void)
{
    int len;
    int * pArr;
    int i;

    //动态构造一维数组
    printf("请输入学生的个数:\n");
    printf("len = ");
    scanf("%d",&len);

    pArr = (struct Student*)malloc(len * sizeof(struct Student));

    //输入
    for(i=0; i<len; i++)
    {
        printf("请输入第%d个学生的信息: \n",i+1);
        printf("age = ");
        scanf("%d",&pArr[i].age);

        printf("name = ");
        scanf("%s",pArr[i].name);

        printf("score = ");
        scanf("%f",&pArr[i].score);
    }
    //输出
    for(i=0; i<len; i++)
    {
        printf("请输入第%d个学生的信息是: \n",i+1);
        printf("age = ");
        printf("%d",pArr[i].age);

        printf("name = ");
        printf("%s",pArr[i].name);

        printf("score = ");
        printf("%f",pArr[i].score);

    return 0;
}
//报错信息 code blocks 16.01
/*
|23|warning: assignment from incompatible pointer type|
|30|error: request for member 'age' in something not a structure or union|
|33|error: request for member 'name' in something not a structure or union|
|36|error: request for member 'score' in something not a structure or union|
|43|error: request for member 'age' in something not a structure or union|
|46|error: request for member 'name' in something not a structure or union|
|49|error: request for member 'score' in something not a structure or union|
|52|error: expected declaration or statement at end of input|
|52|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 7 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
*/
搜索更多相关主题的帖子: name printf for age 输入 
2022-07-22 14:14
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:6 
回复 楼主 xjxjxj
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct Student
{
    int age;
    float score;
    char name[100];
} *pArr;

int main(void)
{
    int len;
    int i;

    //动态构造一维数组
    printf("请输入学生的个数:\n");
    printf("len = ");
    scanf("%d",&len);

    pArr = (struct Student*)malloc(len * sizeof(struct Student));

    //输入
    for(i=0; i<len; i++)
    {
        printf("请输入第%d个学生的信息: \n",i+1);
        printf("age = ");
        scanf("%d",&pArr[i].age);

        printf("name = ");
        scanf("%s",pArr[i].name);

        printf("score = ");
        scanf("%f",&pArr[i].score);
    }
    //输出
    for(i=0; i<len; i++)
    {
        printf("请输入第%d个学生的信息是: \n",i+1);
        printf("age = ");
        printf("%d",pArr[i].age);

        printf("name = ");
        printf("%s",pArr[i].name);

        printf("score = ");
        printf("%f",pArr[i].score);
    }

    return 0;
}


输出部分还有很大的瑕疵,请自行修改

DO IT YOURSELF !
2022-07-22 14:25
xjxjxj
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2022-7-6
收藏
得分:0 
回复 2楼 wp231957
已修改完成
2022-07-22 14:36
快速回复:结构体中,输入输出错 . 和 -> 都不对,错在哪里了 新手求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026624 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved