1.为结构体成员赋值
#include <iostream>
using namespace std;
void main()
{
struct Personlnfo
{
int index;
char name[30];
short age;
}plnfo;
plnfo.index = 0;
plnfo.age = 20;
cout << plnfo.index << endl;
cout << plnfo.age << endl;
system("pause");
}
结果:
#include <iostream>
using namespace std;
void main()
{
struct Personlnfo
{
int index;
char name[30];
short age;
}plnfo = {0,"pangpang",20};
cout << plnfo.index << endl;
cout << plnfo.name << endl;
cout << plnfo.age << endl;
system("pause");
}
结果:
2.使用结构体指针变量引用结构体成员
#include <iostream>
using namespace std;
void main()
{
struct Person
{
int index;
char name[30];
short age;
}*pPersonlnfo, plnfo = { 0,"pangpang",20 };
pPersonlnfo = &plnfo;
cout << pPersonlnfo->index << endl;
cout << pPersonlnfo->name << endl;
cout << pPersonlnfo->age << endl;
system("pause");
}
结果: