C 结构阵列带指针
C++ struct array with pointer
我想随着函数老化(),struct类型数据输入。
我想将数据输入作为结构的数组,并将结构参数作为指针类型,但只能失败(年龄没有增加)
有什么问题?
#include <iostream>
using namespace std;
struct Person
{
int age;
double weight, height;
};
void aging(Person* p);
int main()
{
Person ps[2];
for (int i=0; i<2; i++){
cout <<"age :";
cin >> ps[i].age;
cout <<"weight :";
cin >> ps[i].weight;
cout <<"height :";
cin >> ps[i].height;
}
aging(&ps[2]);
for (int i=0; i<2; i++){
cout <<"age after1: "<<ps[i].age<<"weight after1: "<<ps[i].weight<<"height after1:"<<ps[i].height<<"n";
}
return 0;
}
void aging(Person* p){
p-> age++;
}
它看起来像您在数组ps
的隔离元素上称为 aging
。您将ps
声明为两个元素,但尝试使用ps[2]
访问第三个元素(请记住,C 数组以索引0开始)。如果您想在数组的最后一个元素上调用它,我认为您打算写aging(&ps[1])
。
相关文章:
- EIGEN地图类:将C阵列映射到VectorXD指针以有效的方式
- 通过从当地的C风格阵列中返回指针来获取一个悬空的指针
- 将C 中的每个指针删除作为阵列的指针安全吗?
- 重新分配指针阵列的一部分的安全方法
- 排序阵列会使指针链接错误
- 指针阵列在一个对象数组中函数
- 指针阵列打印值
- C 在指针阵列中删除第一个元素,以效应后来的元素
- 问题将信息复制到指针阵列中
- 从指针打印动态int阵列中获取怪异的字符
- 将指针或一系列指针存储在动态阵列中
- 删除指向排序的字符串阵列的指针会在运行后会有一个错误
- 排序浮动指针阵列
- 快速将双重指针阵列转换为单个指针,可能是连续的
- C 按顺序排列一个指针阵列
- 如何使用struct构造函数在结构中初始化指针阵列
- 带有超载运算符的指针阵列
- 指针阵列和指针数组的指针之间的区别
- 创建类型结构的指针阵列
- 指针阵列的动态初始化