130 likes | 276 Views
Computer Programming Spring-2007. Union in C language Accessing mouse in C language. Union. As structures, unions are also used to group a number of different variables together .
E N D
Computer ProgrammingSpring-2007 Union in C language Accessing mouse in C language
Union • As structures, unions are also used to group a number of different variables together. • The difference between union and structure is that, structure treat each of its member as a different memory location store in the main memory. • While union treat each of its member as a single memory location store in the main memory. • i.e. all of the members of union shares a common memory of union member.
Union Example union searchOption { int SearchByRollNumber; char SearchByName[90]; char SearchByAddress[90]; char SearchByPhoneNumber[90]; }; searchOption sv; void main (void) { int option = 0; switch (option) { case 0: FunSearchRoll (sv.SearchByRollNumber); break; case 1: FunSearchName(sv.SearchByName); break; case 2: FunSearchByAddress(sv.SearchByAddress); break; case 3: FunSearchByPhone(sv.SearchByPhoneNumber); break; } } 90 Bytes
Accessing Mouse in C/C++ • union REGS in,out; void callmouse(){ in.x.ax=1; int86(51,&in,&out);} • int86 (…) is declared in the “dos.h” file, so include this file before calling the function.
Accessing Mouse in C/C++ union REGS { struct WORDREGS x; struct BYTEREGS h; } struct WORDREGS { unsigned int ax, bx, cx, dx; }; struct BYTEREGS { unsigned char al, ah, bl, bh; unsigned char cl, ch, dl, dh; };
Accessing Mouse in C/C++ The various mouse functions can be accessed by setting up the AX register with different values (service number) and issuing interrupt number 51.
Setting Mouse Position • void setposi(int xpos,int ypos) { in.x.ax=4; in.x.cx=xpos; in.x.dx=ypos; int86(51,&in,&out); }
Receiving Mouse Position and Button Status • void mouseposi(int *xpos,int *ypos,int *click) { in.x.ax=3; int86(51,&in,&out); *click=out.x.bx; *xpos=out.x.cx; *ypos=out.x.dx; }
Hiding Mouse • void mousehide() { in.x.ax=2; int86(51,&in,&out); }
Final Code void main (void ) { int xCor,yCor,click,a,b; a=40*8; b=12*8; setposi(a,b); callmouse(); while (1) { mouseposi(&xCor,&yCor,&click); gotoxy(10,9); printf("\n\tMouse Position is: %d,%d",xCor/8,yCor/8); printf("\n\tClick: %d",click); if ( click == 2 ) break; } mousehide();}