【问题标题】:cannot access a structure membre无法访问结构成员
【发布时间】:2016-02-09 19:55:26
【问题描述】:

代码

我正在尝试创建一个程序来管理一所大学,为此我想创建一个stuct student 并在里面放另一个struct branch其中。这是我做的,效果不好

base.h

#define DEG_SIZE 2
#define BRANCH_SIZE 5
struct Branch{
        char Departement;
        char Year;
    }b;
     typedef struct Branch _BRANCH;
_BRANCH branch[BRANCH_SIZE];
   struct Student_informations{
        char Stud_name;
        float deg[DEG_SIZE];
        char* Stud_year;
        char Payment_State;
        _BRANCH Stud_branch;
    }c;
    typedef struct Student_informations _STUDENT;
enum Departement
    { MATH,PHY, 
      CHY,GIO, 
      BIO,INFO
    };
enum Year
    { first,second,
      licence,master,
      doc
    };
enum Payment
    {
        TRUE,
        FALSE
    };

student.c

/*<--------------SETUP-------------->*/

/*=============-MATH-=============*/
            //SMIA
    branch[0].Departement = MATH;
    branch[0].Year = first;

            //SMI/SMA
    branch[1].Departement = MATH;
    branch[1].Year = second;

            //SMF/SMA
    branch[2].Departement = MATH;
    branch[2].Year = licence;

            //MASTER-MATH
    branch[3].Departement = MATH;
    branch[3].Year = master;

            //DOCTURAT-MATH
    branch[4].Departement = MATH;
    branch[4].Year = doc;
/*=============-MATH-=============*/
/*<--------------SETUP-------------->*/ 
#include "base.h"
void set_students(_STUDENT* student)
{
    puts("Enter the Full name");
    scanf("%s", student->Stud_name);
    puts("Enter his deg");
        for(int k=0; k < DEG_SIZE; ++k){
            printf("deg[%d]", k+1);
                scanf("%f",student->deg[k]);
            puts("");
        }// end for
    puts("enter the year");
        scanf("%s", student->Stud_year);
    Stud_branch -> branch[2];
puts("DONE!");
}// end set_students()

af.c

#include "base.h"

/*beginning of main()*/
int main(int argc,char *argv[])
{
    _STUDENT Student;
    set_students(&Student);
    return EXIT_SUCCESS;
}//end main()

正在编译..

[ar.lnx@new-host src-new] $ make
gcc -I.   -c -o af.o af.c
gcc -I.   -c -o student.o student.c
student.c: In function ‘set_students’:
student.c:23:25: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
  student->Payment_State = hold;
                         ^
Building the application core..
gcc -o x af.o student.o -I.
Finish.
[ar.lnx@new-host src-new] $

正在执行

[ar.lnx@new-host src-new] $ ./x
10
Enter the Full name
anas
Segmentation fault (core dumped)
[ar.lnx@new-host src-new] $

我尝试了两天来解决这个问题,任何人都可以帮助我了解问题出在哪里以及如何解决它

【问题讨论】:

  • student.c 不完整。特别是,我没有看到产生警告的代码行。
  • 标准保留以_ 和大写字母开头的标识符。 不要在应用程序代码中使用它们。

标签: c pointers struct scanf


【解决方案1】:

第一个问题在

  scanf("%s", student->Stud_name);

在您的情况下,Stud_name 是一个 char,不足以容纳 字符串,(更不用说您错过了传递地址部分)。你需要一个数组。

尝试使用%s 格式说明符将字符串 读入char 调用undefined behavior

尝试将Stud_name的定义从

char Stud_name;

#define NAMESIZ 32
. . . . 
char Stud_name[NAMESIZ];

然后,回到我之前离开的点,您需要将变量的地址作为格式说明符的参数传递给scanf()。类似的东西

 scanf("%f",&(student->deg[k]));

等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2023-03-16
    • 2013-05-31
    • 2016-07-20
    • 1970-01-01
    相关资源
    最近更新 更多