标题:结构体中,输入输出错 . 和 -> 都不对,错在哪里了 新手求助
只看楼主
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: 16Rank: 16Rank: 16Rank: 16
来 自:神界
等 级:版主
威 望:422
帖 子:13681
专家分:53296
注 册: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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-509634-1-1.html




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

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