C 如何创建联盟

C 如何创建联盟,c,unions,C,Unions,我在使用union时犯了一个错误,我不知道为什么。 问题出在函数goto_xy() 我从书上读到的,但它不能被编译。 在这个函数中,我试图定位光标,但未声明REGS变量。我想知道它的功能是什么 #include<stdio.h> #include<windows.h> #include<dos.h> #include<conio.h> void goto_xy(int x,int y); //got

我在使用union时犯了一个错误,我不知道为什么。 问题出在函数
goto_xy()
我从书上读到的,但它不能被编译。
在这个函数中,我试图定位光标,但未声明
REGS
变量。我想知道它的功能是什么

#include<stdio.h>
#include<windows.h>
#include<dos.h>
#include<conio.h>

void goto_xy(int x,int y);                         //goto is a key word;define the subfunction to creat the original cursor int the coordinate system
void rectangle_clear(int x1,int x2,int y1,int y2); //define the rectangle_clear opening subfunction 
void center_clear(int x1,int x2,int y1,int y2);    //define the center_clear opening subfunction
void creat();                                  //define the subfunction of creating the star
int main()                                             //the main function
{
    creat();
    getch();
    center_clear(0,25,0,79);
    getch();
}
void center_clear(int x1,int x2,int y1,int y2)     //the subfunction which creats the stars while opening the project
{
    int x00,y00,x0,y0,i,d;
    if((y2-y1)>(x2-x1))
    {
        d=(x2-x1)/2;
        x0=(x1+x2)/2;
        y0=y1+d;
        y00=y2-d;
        for(i=0;i<(d+1);i++)
        {
            rectangle_clear((x0-i),(x00+i),(y0-i),(y00+i));
        }
        delay(10);                                  //to delay the dismis of the star
    }
    else
    {
        d=(y2-y1)/2;
        y0=(y1+y2)/2;
        x0=x1+d;
        x00=x2-d;
        for(i=0;i<d+1;i++)
        {
            rectangle_clear((x0-i),(x00+i),(y0-i),(y00+i));
        }
        delay(10);
    }
}
void rectangle_clear(int x1,int x2,int y1,int y2)   //to creat the star int the shape of a rectangle
{
    int i,j;
    for(i=y1;i<y2;i++)
    {
        goto_xy(x1,i);
        putchar(' ');
        goto_xy(x2,i);
        putchar(' ');
        delay(10);
    }
    for(j=x1;j<x2;j++)
    {
        goto_xy(i,y1);
        putchar(' ');
        goto_xy(i,y2);
        putchar(' ');
        delay(10);
    }
}
void goto_xy(int x,int y)
{
    union REGS r;

    r.h.ah=2;
    r.h.dl=y;
    r.h.dh=x;
    r.h.bh=0;
    int86(0x10,&r,&r);
}
void creat()
{
    int i,j;
    for(i=0;i<24;i++)
    {
        for(j=0;j<79;j++)
        {
            goto_xy(i,j);
            printf("a");
        }
    }
}
#包括
#包括
#包括
#包括
无效goto_xy(整数x,整数y)//后藤是一个关键词;定义子函数以在坐标系中创建原始光标
空矩形_清除(int-x1、int-x2、int-y1、int-y2)//定义矩形\u清除打开子功能
空隙中心_清除(int x1、int x2、int y1、int y2)//定义“中心\清除打开”子功能
void creat()//定义创建星号的子功能
int main()//主函数
{
创建();
getch();
中心清晰(0,25,0,79);
getch();
}
void center_clear(intx1,intx2,inty1,inty2)//打开项目时创建星星的子功能
{
int x00,y00,x0,y0,i,d;
如果((y2-y1)>(x2-x1))
{
d=(x2-x1)/2;
x0=(x1+x2)/2;
y0=y1+d;
y00=y2-d;

对于(i=0;i要定义联合体,您需要执行以下操作:

union REGS{some_type h;other_type f;};


现在,您可以创建类型为
REGS
的变量并使用union。

在我看来,union
REGS
必须已经存在于其中一个头文件中,并且您正在包括相同的头文件

从下面的代码中可以看出,即使是像
h
h
这样的联盟成员也存在,这意味着联盟存在于某些头文件中,您将其包括在内

void goto_xy(int x,int y)
{
    union REGS r;

    r.h.ah=2; //Here you are accessing the member of REGS and even the sub-members of h
    r.h.dl=y;
    r.h.dh=x;
    r.h.bh=0;
    int86(0x10,&r,&r);
}
编辑: 谷歌搜索告诉我,
UNION REGS
将在
dos.h
中定义,类似于

union REGS { 
 struct WORDREGS x;
 struct BYTEREGS h;
};
因此,您需要包括dos.h来解决您的问题。但是,尽管您也包括在内,但这个问题仍然存在。您也可以打开dos.h并检查是否存在联合注册表


有关更多详细信息,请参阅。

编译错误是什么?这个问题相当于您问什么是
REGS
。好吧,这是您的程序?您告诉我们!请注意,您同时使用的是windows.h标头和dos.h标头。我觉得这不对,您使用的是MS windows或MS dos。