/* Program STRUCTURE-TEST #0 Basic structure handling Version 1.0 / 2001.10.01 Autor: Peter Schindler, pstrainer@gmx.net */ /* compiler option: version 1...4 */ #define version 1 /* security check */ #ifndef version #define version 1 #endif #if ((version<1)||(version>4)) #define version 1 #endif /* standard libraries */ #include #include /* structure definition */ struct teststruc { int sernr; int demo; }; /* function definitions in different versions */ void print_header (char *); void print_trailer (void); #if (version<=1) void printstruc (teststruc); void changestruc (teststruc); #else void printstruc (teststruc *); void changestruc (teststruc *); #endif /********** main **********/ void main (void) { char myname[]="STRUCTURE-TEST #0"; // declare tsa of type teststruc teststruc tsa; print_header(myname); /* how to handle structure variables */ tsa.sernr = 90; tsa.sernr+= 9; tsa.demo= 900 + tsa.sernr; /* print structure, change it, print again in 4 versions: */ #if (version<=1) // copy structure to sub-function printstruc (tsa); changestruc (tsa); printstruc (tsa); #else // copy pointer to structure to sub-function printstruc (&tsa); changestruc (&tsa); printstruc (&tsa); #endif print_trailer(); } /* functions printstruc and changestruc in 4 different versions */ #if (version<=1) // we get a copy (!) of the structure itself: void printstruc (teststruc a) { int i,j; printf("version %d: ",version); i=a.sernr; j=a.demo; printf("i=%d j=%d\n",i,j); } void changestruc (teststruc a) { a.sernr-=9; a.demo-=99; } #endif #if (version==2) // we get a pointer to the structure // we copy the structure to our own structure // we work with our own structure // we copy the result to the original structure void printstruc (teststruc *pa) { int i,j; teststruc b; printf("version %d: ",version); b=*pa; i=b.sernr; j=b.demo; printf("i=%d j=%d\n",i,j); } void changestruc (teststruc *pa) { teststruc b; b=*pa; b.sernr-=9; b.demo-=99; *pa=b; } #endif #if (version==3) // we get a pointer to the structure // we work with the structure using pointers void printstruc (teststruc *pa) { int i,j; printf("version %d: ",version); i=(*pa).sernr; j=(*pa).demo; printf("i=%d j=%d\n",i,j); } void changestruc (teststruc *pa) { (*pa).sernr-=9; (*pa).demo-=99; } #endif #if (version>=4) // we get a pointer to the structure // we work with the structure using -> pointers void printstruc (teststruc *pa) { int i,j; printf("version %d: ",version); i=pa->sernr; j=pa->demo; printf("i=%d j=%d\n",i,j); } void changestruc (teststruc *pa) { pa->sernr-=9; pa->demo-=99; } #endif void print_header (char *header) { #ifdef _DEBUG printf("Debug version "); #else printf("Release version "); #endif printf("of program %s\n\n",header); } void print_trailer (void) { #ifdef _DEBUG /* show console window until key pressed */ printf("\nPress any key to continue "); _getch(); #endif } /* ========== eof ========== */