如何在C++中初始化结构数组并用指针访问每个元素
How to initialize Array of Structure and Access each element with Pointer in C++
在下面的程序中,我遇到了结构初始化问题。初始化后,我需要使用Pointer对每个结构元素进行定制。
#include <iostream>
using namespace std;
struct student{
int rollno;
float marks;
char name[45];
}*ptr;
int main(){
//Getting initialization error
student s1[2]={1,50.23,"abc",2,65.54,"def"};
for(int i=0;i<2;i++){
//Need to cout using pointers
cout<<s1[i].rollno<<s1[i].marks<<s1[i].name;
}
return 0;
}
您应该在{}
:中包装数组的每个成员
student s1[2]={{1,50.23,"abc"},{2,65.54,"def"}};
相关文章:
- 通过指向指针数组的指针访问子类的属性
- 使用指针访问数组中的对象数据成员
- 无法通过指针访问对象的成员
- decltype:使用指针访问类的静态成员
- 是否可以使用指针访问变量以避免直接编辑变量?
- 为什么 C++ 地址中的矢量无法通过原始指针访问
- 使用指针访问数组元素时出现意外结果
- 使用指针访问数组的值
- C++继承从基类指针访问派生类中的非虚拟函数
- 如何使用指针访问成对向量的元素
- 为什么有些库提供无指针访问,而另一些库则不提供
- 如何通过引用或指针访问二维矢量
- 使用指针访问参数接收的结构中的元素时内存泄漏
- 我正在学习C++,我不能使用指针访问参考吗?(举个例子)
- 复制堆栈上的成员值或使用指针访问它?
- C++娜娜 如何从全局列表框*指针访问列表框内的索引 0 上的按钮?
- 通过此指针访问时的成员差异和自身的成员指针(简单的 QT 示例问题)
- 从指针访问 Kaguya Lua 状态
- 说明通过指针访问非静态类成员函数
- 有没有办法使用初始化到第一行的指针访问 2d 数组的第二行?