指向结构的指针中的运行时错误 我在C++中探索结构中的所有可能性,当我指向结构指针时,我遇到了一些运行时错误。

指向结构的指针中的运行时错误 我在C++中探索结构中的所有可能性,当我指向结构指针时,我遇到了一些运行时错误。,c++,pointers,structure,C++,Pointers,Structure,这是我的密码: #include<iostream> #include<string> using namespace std; // globally defined structure can be used in any method struct stud { int roll; string sname; // nested structure for date of birth struct dob {

这是我的密码:

    #include<iostream>
#include<string>
using namespace std;

// globally defined structure can be used in any method
struct stud
{
    int roll;
    string sname;
    // nested structure for date of birth
    struct dob
    {
        int date;
        int month;
        int year;
    } dob1;
    int marks1;
    int marks2;
    int marks3;
} stud1 = {1,"pranshu1",{11,4,1993},65,87,80};

// function to calculate average of a student
float calaverage(stud s1)
{
    float savg=s1.marks1+s1.marks2+s1.marks3;
    savg=savg/3;
    return savg;
}

// function to calculate the average of whole class
// function parameter are the starting address of the array of the structure stud 
// and the total no of elements in the array
float avgclass(stud *ptr, int no_of_stud)
{
    float classtotal=0;
    for(int i=0;i<no_of_stud; i++)
    {
        classtotal=classtotal+calaverage(*(ptr+i));     
    }
    return classtotal;
}

int main()
{
    // array of structure
    struct stud class1[3]=
    {
    stud1,
    {2,"pranshu2",{11,4,1993},80,95},   
    {3,"pranshu3",{11,4,1993},64,84,93}
    };
    // initializing the value by accessing the member
    class1[0].marks3=93;
    struct stud* stud5;
    stud5->roll=10;
    stud5->sname="pranshu";
    stud5->dob1.date=11;
    stud5->dob1.month=4;
    stud5->dob1.year=93;
    stud5->marks1=97;
    stud5->marks2=45;
    stud5->marks3=98;


    cout<<stud5->roll<<endl;
    // average marks of student 1
    cout<<"Average marks of "<<stud1.sname<<" with roll no "<<stud1.roll<<" is "<<calaverage(stud1)<<endl;

    // class average
    cout<<"Class average is "<<avgclass(class1,3)<<endl;
}
#包括
#包括
使用名称空间std;
//全局定义的结构可以用于任何方法
结构螺柱
{
整流罩;
弦圈套;
//出生日期的嵌套结构
结构dob
{
国际日期;
整月;
国际年;
}dob1;
int标记1;
int标记2;
int标记3;
}stud1={1,“pranshu1”,{11,41993},65,87,80};
//用于计算学生平均成绩的函数
浮子平均值(螺柱s1)
{
float savg=s1.marks1+s1.marks2+s1.marks3;
savg=savg/3;
返回savg;
}
//函数来计算整个类的平均值
//函数参数是结构螺柱数组的起始地址
//以及数组中的元素总数
浮动平均等级(螺柱*ptr,螺柱内部编号)
{
float classtotal=0;
对于(inti=0;iroll=10;
stud5->sname=“pranshu”;
stud5->dob1.date=11;
stud5->dob1.month=4;
研究5->dob1。年=93;
stud5->marks1=97;
stud5->marks2=45;
stud5->marks3=98;

cout
struct stud*stud5;
只是一个指针……您还应该分配内存:

stud5 = new stud;

有一个对象。< /P>所以不是在线调试服务!请在您问之前提出自己的问题。在C++中,在声明变量时不需要重复<代码>结构> /代码>。