结构体指针相关问题
#include <iostream>using namespace std;
struct Student
{
char studentName;
int score;
};
struct Team
{
char teacher;
struct Student std;
};
void getinfo(struct Team *p, int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "请输入指导老师的姓名:";
cin >> p.teacher; //请问为什么我输入的是一个结构体指针赋值的时候不该是用 -> 吗 为什么这里要用.呢
for (j = 0; j < 3; j++)
{
cout << "学生"<<j+1<<endl;
cout << "姓名: ";
cin >> p.std.studentName;
cout << "分数: ";
cin >> p.std.score;
}
cout << endl;
}
cout << endl;
cout << "--------信息录入完毕!--------" << endl;
cout << endl;
}
void print(struct Team *p, int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "指导老师: ";
cout << p.teacher<<endl;
for (j = 0; j < 3; j++)
{
cout << "学生" << j+1 << endl;
cout << "姓名:";
cout << p.std.studentName<<"----";
cout << "分数:";
cout << p.std.score <<endl;
}
cout << endl;
}
}
int main(void)
{
int len;
struct Team team;
len = sizeof(team) / sizeof(team);
getinfo(team, len);
print(team, len);
system("pause");
return 0;
} 编译成功吗?能运行? Max472 发表于 2021-9-6 17:11
编译成功吗?能运行?
用->编译报错 用.的话可以运行 Max472 发表于 2021-9-6 17:11
编译成功吗?能运行?
KillerReborn 发表于 2021-9-6 17:18
你把参数里边的 struct 删了,留下 Team *p,试试能不能用 "->" Max472 发表于 2021-9-6 17:27
你把参数里边的 struct 删了,留下 Team *p,试试能不能用 "->"
还是不行 报错这个
严重性 代码 说明 项目 文件 行 禁止显示状态
错误(活动) E3364 运算符 -> 或 ->* 应用于 "Team" 而不是指针类型 本帖最后由 jhq999 于 2021-9-6 17:41 编辑
p是指针,p已经是结构体对象实体了,不是指针,p->可不就错了 KillerReborn 发表于 2021-9-6 17:30
还是不行 报错这个
严重性 代码 说明 项目 文件 行 禁止显示状态
错误(活动) E3364 运算符 -> 或 ->*...
vs2019,我把你的用 “.” 运算符的代码复制报错,改成了 “->” 就行啦,没有问题,能运行 jhq999 发表于 2021-9-6 17:31
p是指针,已经是结构体对象实体了,不是指针,可不就错了
#include <iostream>
using namespace std;
struct Student
{
char studentName;
int score;
};
struct Team
{
char teacher;
struct Student std;
};
void getinfo(struct Team *p[], int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "请输入指导老师的姓名:";
cin >> p->teacher;
for (j = 0; j < 3; j++)
{
cout << "学生"<<j+1<<endl;
cout << "姓名: ";
cin >> p->std.studentName;
cout << "分数: ";
cin >> p->std.score;
}
cout << endl;
}
cout << endl;
cout << "--------信息录入完毕!--------" << endl;
cout << endl;
}
void print(struct Team *p[], int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "指导老师: ";
cout << p->teacher<<endl;
for (j = 0; j < 3; j++)
{
cout << "学生" << j+1 << endl;
cout << "姓名:";
cout << p->std.studentName<<"----";
cout << "分数:";
cout << p->std.score <<endl;
}
cout << endl;
}
}
int main(void)
{
int len;
struct Team *team;
len = sizeof(team) / sizeof(team);
getinfo(team, len);
print(team, len);
system("pause");
return 0;
}
我改成生成结构体指针数组后 没报错了 但是运行编译输入后 又报错了
Max472 发表于 2021-9-6 17:52
vs2019,我把你的用 “.” 运算符的代码复制报错,改成了 “->” 就行啦,没有问题,能运行
看他图片的第23行,他不用<>发代码,被吞了 本帖最后由 jhq999 于 2021-9-6 18:05 编辑
用这个<>发代码 jhq999 发表于 2021-9-6 18:00
看他图片的第23行,他不用发代码,被吞了
前面的问题明白了 后面 我用结构体指针数组 编译后为什么不能运行呢 本帖最后由 KillerReborn 于 2021-9-6 18:10 编辑
jhq999 发表于 2021-9-6 18:04
用这个发代码
#include <iostream>
using namespace std;
struct Student
{
char studentName;
int score;
};
struct Team
{
char teacher;
struct Student std;
};
void getinfo(struct Team *p[], int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "请输入指导老师的姓名:";
cin >> p->teacher;
for (j = 0; j < 3; j++)
{
cout << "学生"<<j+1<<endl;
cout << "姓名: ";
cin >> p->std.studentName;
cout << "分数: ";
cin >> p->std.score;
}
cout << endl;
}
cout << endl;
cout << "--------信息录入完毕!--------" << endl;
cout << endl;
}
void print(struct Team *p[], int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "指导老师: ";
cout << p->teacher<<endl;
for (j = 0; j < 3; j++)
{
cout << "学生" << j+1 << endl;
cout << "姓名:";
cout << p->std.studentName<<"----";
cout << "分数:";
cout << p->std.score <<endl;
}
cout << endl;
}
}
int main(void)
{
int len;
struct Team *team;
len = sizeof(team) / sizeof(team);
getinfo(team, len);
print(team, len);
system("pause");
return 0;
} KillerReborn 发表于 2021-9-6 18:08
前面的问题明白了 后面 我用结构体指针数组 编译后为什么不能运行呢
你用编辑器里边的 "<>"把代码发上来 Max472 发表于 2021-9-6 18:10
你用编辑器里边的 ""把代码发上来
#include <iostream>
using namespace std;
struct Student
{
char studentName;
int score;
};
struct Team
{
char teacher;
struct Student std;
};
void getinfo(struct Team *p[], int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "请输入指导老师的姓名:";
cin >> p->teacher;
for (j = 0; j < 3; j++)
{
cout << "学生"<<j+1<<endl;
cout << "姓名: ";
cin >> p->std.studentName;
cout << "分数: ";
cin >> p->std.score;
}
cout << endl;
}
cout << endl;
cout << "--------信息录入完毕!--------" << endl;
cout << endl;
}
void print(struct Team *p[], int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "指导老师: ";
cout << p->teacher<<endl;
for (j = 0; j < 3; j++)
{
cout << "学生" << j+1 << endl;
cout << "姓名:";
cout << p->std.studentName<<"----";
cout << "分数:";
cout << p->std.score <<endl;
}
cout << endl;
}
}
int main(void)
{
int len;
struct Team *team;
len = sizeof(team) / sizeof(team);
getinfo(team, len);
print(team, len);
system("pause");
return 0;
} 本帖最后由 jhq999 于 2021-9-6 18:49 编辑
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Student
{
char studentName;
int score;
};
struct Team
{
char teacher;
struct Student std;
};
void getinfo(struct Team *p, int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "请输入指导老师的姓名:";
cin >> p.teacher;
for (j = 0; j < 3; j++)
{
cout << "学生"<<j+1<<endl;
cout << "姓名: ";
cin >> p.std.studentName;
cout << "分数: ";
cin >> p.std.score;
}
cout << endl;
}
cout << endl;
cout << "--------信息录入完毕!--------" << endl;
cout << endl;
}
void print(struct Team *p, int len)
{
int i, j;
for (i = 0; i < len; i++)
{
cout << "指导老师: ";
cout << p.teacher<<endl;
for (j = 0; j < 3; j++)
{
cout << "学生" << j+1 << endl;
cout << "姓名:";
cout << p.std.studentName<<"----";
cout << "分数:";
cout << p.std.score <<endl;
}
cout << endl;
}
}
int main(void)
{
int len;
struct Team team;//只给个菜单不上菜怎么行?或者 struct Team* team=newstruct Team;后面别忘了delete的好习惯
len = sizeof(team) / sizeof(team);
getinfo(team, len);
print(team, len);
system("pause");
return 0;
} //#include "stdafx.h" //这个头文件emmmm,我给你注了
#include <iostream>
using namespace std;
struct Student
{
char studentName;
int score;
};
struct Team
{
char teacher;
Student std;
};
void getinfo(Team* p, int len)
{
for (int i = 0; i < len; i++)
{
cout << "请输入指导老师的姓名:";
cin >> (p+i)->teacher;
for (int j = 0; j < 3; j++)
{
cout << "学生" << j + 1 << endl;
cout << "姓名: ";
cin >> ((p+i)->std+j)->studentName;
cout << "分数: ";
cin >> ((p+i)->std+j)->score;
}
cout << endl;
}
cout << endl;
cout << "--------信息录入完毕!--------" << endl;
cout << endl;
}
void print(Team* p, int len)
{
for (int i = 0; i < len; i++)
{
cout << "指导老师: ";
cout << p.teacher << endl;
for (int j = 0; j < 3; j++)
{
cout << "学生" << j + 1 << endl;
cout << "姓名:";
cout << p.std.studentName << "----";
cout << "分数:";
cout << p.std.score << endl;
}
cout << endl;
}
}
int main(void)
{
int len;
Team team;
len = sizeof(team) / sizeof(team); //其实这里不用算,你用的数组本来就是个常量
getinfo(team, len);
print(team, len);
system("pause");
return 0;
}
整体代码奇奇怪怪,为了区别第一个函数我给你用的跳指针的方式改的,第二个函数用的下标运算符的方式给你改的,应该是你想要的效果了 liuxinjordon 发表于 2021-9-6 23:04
整体代码奇奇怪怪,为了区别第一个函数我给你用的跳指针的方式改的,第二个函数用的下标运算符的方式给你 ...
好的 十分感谢
页:
[1]