c++联合和结构表
有人告诉我编写一个程序,创建一个联合和结构,然后创建联合和结构的二元素数组并填充它们的字段。我创建了一个联合和一个结构,但是如何将它们的字段填充到数组中?
#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;
}
我试过 un[0]->i1=i;但这不是执行此操作的正确方法。
I was told to write a program, that creates a union and structure, then creates two-element arrays of unions and structures and fills their fields. I have created a union and a structure, but how to fill their fields in arrays ?
#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;
}
I've tried un[0]->i1=i; but it's not the proper way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
un
是一个complex
数组,而不是一个指向complex
的指针数组。因此,un[0]
是一个复数
,而不是指向复数
的指针。因此,您需要:
名为
u
的complex
类型的全局实例看起来有点毫无意义,可能应该删除。un
is an array ofcomplex
, not an array of pointers tocomplex
. Therefore,un[0]
is acomplex
, not a pointer to acomplex
.Thus, you need:
The global instance of type
complex
calledu
looks a bit pointless, and should probably be removed.