new*_*per 2 c++ structure unions
我被告知编写一个程序,该程序创建一个并集和结构,然后创建由两个元素组成的并集和结构数组,并填充它们的字段。我创建了一个并集和一个结构,但是如何将它们的字段填充为数组?
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
union complex;
union complex{
int i1;
long double ld1;
} u;
struct Person {
char* name;
int age;
bool sex;
void show(){
printf("name %s, age %2.0d, sex %1d\n",
name , age, sex);
};
} person;
int main(void)
{
Person *o = new Person[2];
complex *un = new complex[2];
un[0]->i1=i;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试过un [0]-> i1 = i; 但这不是正确的方法。
un是的数组complex,而不是的指针数组complex。因此,un[0]是complex,而不是的指针complex。
因此,您需要:
un[0].i1 = i;
Run Code Online (Sandbox Code Playgroud)
complex名为type的全局实例u看起来毫无意义,应该删除。
| 归档时间: |
|
| 查看次数: |
201 次 |
| 最近记录: |