これは、4 つの異なる構造を持つユニオン テンプレートで構成されるヘッダー ファイルの 1 つです。
#define MAX 3
union family
{
struct name /*for taking the name and gender of original member*/
{
unsigned char *namess;
unsigned int gender;
union family *ptr_ancestor; /*this is a pointer to his ancestors details*/
}names;
struct male /*for taking the above person's 3 male ancestors details if he is male*/
{
unsigned char husb_names[3][20];
unsigned char wife_names[3][20];
unsigned int wife_status[3];
}male_ancestor;
struct unmarry /*for taking the above person's 3 female parental ancestors if she is female and unmarried*/
{
unsigned int mar;
unsigned char parental_fem[3][20];
unsigned int marit_status[3];
}fem_un;
struct marry /*for taking 3 parental-in-laws details if she is female and married*/
{
unsigned int mar;
unsigned char in_law_fem[3][20];
unsigned int in_marit_status[3];
}fem_marr;
};
extern union family original[MAX]; /*for original person*/
extern union family ancestor_male[MAX]; /*used if he is male for storing his male ancestor details*/
extern union family ancestor_female[MAX]; /*used if she is female*/
extern int x;
私の目的は、次のように人の名前と性別を取得し、その人の性別と婚姻状況に従って、その人の任意の 3 つの男性/女性の祖先を保存することです..
つまり、MAX
3 つのメンバーがあり、それぞれに 3 つの先祖がいるということです。これらの祖先は、次の条件のように、対応するメンバーの性別によって決定されます。
- 男性なら使う
struct male
- 女性未婚の場合
struct unmarry
- 女性既婚の場合
struct marry
struct name
*ptr_ancestor
は、祖先を取得し、対応する祖先配列 (ancestomale か祖先女性)を指す必要があるメンバーの名前と性別用です。
メモリ内のオブジェクトは共用体です。Ok。実際、私のプログラムには共用体の配列があります。配列の各要素は、共用体で異なる構造を使用している可能性があります。ここでは、ポインタの割り当てに注意する必要があります。そうしないと、実行時に古い人のレコードが失われる可能性があります。
可能であれば、最初の要素の詳細を取得する方法を教えてください。original[0]
服用後もoriginal[1]
。ここでは、配列の最後の要素を取得しているだけで、以前のすべてのレコードは実行時に削除されています。他のデータ構造やファイルは使用していません。
私の環境は Windows の Turbo C です。