home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / code_examples / cmanual_456 / introduction / introduction.doc < prev    next >
Text File  |  1990-02-07  |  35KB  |  1,123 lines

  1. 0    INTRODUCTION
  2.  
  3. 0.1  INTRODUCTION
  4.  
  5. In this chapter we will look at how C compilers work, how to
  6. use the Lattice C compiler, repeat some important things in C,
  7. and finish of by explaining how two use the Amigas's Libraries,
  8. and discuss the three different types of memory. As you have
  9. noticed, this chapter will give you a short introduction to C,
  10. compilers and the Amiga.
  11.  
  12.  
  13.  
  14. 0.2  HOW TO COMPILE AND LINK
  15.  
  16. The idea with C compilers is that you write a document
  17. (usually referred as "source code"), which is read by the
  18. C compiler in two stages. The first time the C compiler will
  19. create a working file, referred as "quad file". The quad file
  20. is then compiled a second time into an "object file".
  21.  
  22. The object file contains mostly executable machine code, but it
  23. also contains references to external functions/variables in
  24. other object files and libraries. So before we can run the
  25. program we need to tie all object files and libraries together
  26. into one single program. It is done by the linker.
  27.  
  28.  
  29. -------------
  30. | Program.c |   Source Code
  31. -------------
  32.       |
  33.       |         1. First phase of compilation (lc1) 
  34.       V
  35. -------------
  36. | Program.q |   Quad file
  37. -------------
  38.       |
  39.       |         2. Second phase of compilation (lc2)
  40.       V
  41. -------------
  42. | Program.o |   Object file
  43. -------------
  44.       |
  45.       |         3. Linking (blink)
  46.       V
  47. -------------
  48. |  Program  |   Executable program
  49. -------------
  50.  
  51.  
  52.  
  53. 0.2.1  SOURCE CODE
  54.  
  55. When you are going to write C programs you need an editor/
  56. wordprocessor to create the "source code". The source code is
  57. like a normal document except that it is written following some
  58. special rules. Almost any kind of editor/wordprocessor can be
  59. used. You can even use the MEMACS which you got when you bought
  60. your Amiga. (MEMACS exist on "The Extras Disk".)
  61.  
  62. Here is an example of a source code:
  63.  
  64. #include <stdio.h>
  65.  
  66. main()
  67. {
  68.   printf("Hello!\n");
  69. }
  70.  
  71. Once you have written the source code you need to save it. It
  72. is standard that the file names should end with the extension
  73. ".c". So if you want to call your program "hello", the source
  74. code should be named "hello.c".
  75.  
  76.  
  77.  
  78. 0.2.2  FIRST PHASE OF COMPILATION
  79.  
  80. Once the source code is saved you can start the first phase of
  81. compilation. If you use the Lattice C Compiler you simply use
  82. the command "lc1". To compile our example you only need to
  83. write "lc1 hello.c", and the compiler will then create a quad
  84. file named "hello.q".
  85.  
  86.  
  87.  
  88. 0.2.3  SECOND PHASE OF COMPILATION
  89.  
  90. To start the second phase of compilation you use the command
  91. "lc2". To compile our example you then only need to write
  92. "lc2 hello.q", and the compiler will then create an object file
  93. named "hello.o".
  94.  
  95. If you want to invoke both the first and second compiler pass
  96. in one single call you can use the command "lc". So if you
  97. write "lc hello.c" both the first and the second phase of
  98. compilation would be executed. (lc calls both lc1 and lc2
  99. automatically.)
  100.  
  101.  
  102.  
  103. 0.2.4  LINKING
  104.  
  105. The quad file does not, as said before, contain only machine
  106. code instructions. It also contains calls to external functions
  107. in other object files and libraries. Before you may run the
  108. program you therefore need to tie everything together by a
  109. linker. Lattice C uses a linker called "blink".
  110.  
  111. Every program you create must normally be linked together with
  112. the startup routine "c.o", and the two libraries: "lc.lib" and
  113. "amiga.lib".
  114.  
  115. The object file and the two libraries are placed in the "lib"
  116. directory on the second Lattice C disk. That directory is
  117. normally already assigned the name "LIB:", so if you want to
  118. find the files, you simply write "LIB:c.o" for example.
  119.  
  120. For almost all programs you call the linker like this:
  121. (This shows how to link our example "hello".)
  122.  
  123. blink LIB:c.o, hello.o  TO hello  LIB LIB:lc.lib, LIB:amiga.lib
  124.         [A]      [B]       [C]    [D]     [E]          [F]
  125.  
  126.  
  127. [A] The object file "c.o" must always be placed before all
  128.     other object files. The file can be found in the logical
  129.     device "LIB:".
  130.  
  131. [B] After the startup file "c.o" you may place all other
  132.     object files you want to link together. (Remember to put
  133.     a comma between them.) In this example we only have one
  134.     object file, "hello.o".
  135.  
  136. [C] After the list of object files you write the word "TO" plus
  137.     a name which will be the name of the executable program. In
  138.     this example we want our program to be called "hello".
  139.  
  140. [D] Almost all programs need to be linked together with some
  141.     libraries. The keyword "LIB" tells blink that the coming
  142.     file-names are names on libraries. (Remember to put a comma
  143.     between each library name.)
  144.     
  145.     Do not mix up the keyword "LIB" with the logical device
  146.     "LIB:". "LIB" tells the linker that a list of library-names
  147.     will come, while the logical device "LIB:" tells AmigaDOS
  148.     which device and directory to find some files in.
  149.  
  150. [E] The library "lc.lib" must be the second last library in the
  151.     list. The library can be found in the logical device
  152.     "LIB:".)
  153.  
  154. [F] The library "amiga.lib" must be the last library in the
  155.     list. The library can be found in the logical device
  156.     "LIB:".)
  157.  
  158.  
  159. If you want to invoke both the first and second compiler pass,
  160. plus link the object file together with the standard libraries,
  161. in one single call you can use the command "lc" with the added
  162. parameter "-L". So if you want to compile and link the program
  163. in one single command you only need to write:
  164.  
  165. lc -L hello.c
  166.  
  167. The command "lc" will invoke both the first and the second
  168. compiler pass, and the added parameter "-L" means that the
  169. finished object code should be linked together with the
  170. standard libraries.
  171.  
  172. If you also want the linker to search the standard math library
  173. you simply put the letter "m" after the "-L":
  174.  
  175. lc -Lm <file-name.c>
  176.  
  177. Our simple example "hello" does not need the math library, but
  178. if you use any sort of calculations you normally need to
  179. include it. ALL examples in this manual will compile and link
  180. happily with the command:
  181.  
  182. lc -Lm ExampleX.c
  183.  
  184.  
  185.  
  186. 0.3  C PROGRAMS
  187.  
  188. This manual is written for everyone who has a little knowledge
  189. of C and who wants to program the Amiga. If you know how to
  190. write a program that prints out "hello!" on the screen, and
  191. are not totally confused by pointers and structures, I do not
  192. think you will have any problems reading this manual. All
  193. examples have been written as clearly as possible, and I have
  194. avoided many shortcuts in order to make the examples easily
  195. understandable.
  196.  
  197. However, before we start with the real manual I think it is
  198. best to repeat some important features in C:
  199.  
  200.  
  201.  
  202. 0.3.1  #INCLUDE
  203.  
  204. The first thing you find in a C program is the "#include"
  205. commands. They tell the compiler to read some files before
  206. compiling the code, and can therefore contain definitions of
  207. constants/structures/macros etc. The files are normally
  208. referred as "header files", and have because of that a ".h"
  209. extension.
  210.  
  211. There exist two types of header files; your own and standard
  212. system definitions. If you include your own header files you
  213. put a double-quotation around the file name:
  214.  
  215. #include "mydefinitions.h"
  216.  
  217. The compiler will then look in the "current directory" for your
  218. file. If you on the other hand include any standard system
  219. definitions you put two angle-brackets around the file name:
  220. (The compiler will then look in the logical device "INCLUDE:")
  221.  
  222. #include <intuition/intuition.h>
  223.  
  224.  
  225. Note that there is not semicolons after the #include
  226. statements! That is because the #include files are scanned
  227. by a macro pre-processor (included in the first compiler phase,
  228. lc1), and have actually nothing to do with the C compiler.
  229.  
  230.  
  231. Later in the manual we will often include the file intuition.h
  232. which contains definitions of many structures (such as Window,
  233. Border, Gadget etc) plus some important constants and macros.
  234. This and all other standard system header files are compressed
  235. and put on the second Lattice C disk. If you are interested to
  236. see what they actually define you can find the uncompressed
  237. files on the fourth Lattice C disk.
  238.  
  239.  
  240.  
  241. 0.3.2  #DEFINE
  242.  
  243. After the #include statements the #define statements will come.
  244. With help of the #define command you can both declare constants
  245. and replacement strings, but you can even declare your own
  246. macros.
  247.  
  248. The important thing to remember is that the first defined
  249. string is replaced with the second string by the macro pre-
  250. processor: (The first space will separate the first string from
  251. the second string. Note the underline character between MAX and
  252. WIDTH, and the quotations between Anders Bjerin.)
  253.  
  254. #define MAX_WIDTH 150
  255. #define AUTHOR    "Anders Bjerin"
  256.  
  257. The macro pre-processor will replace all MAX_WITH strings with
  258. 150, and all AUTHOR strings with "Anders Bjerin".
  259.  
  260.  
  261. If there are any parentheses in the first string then you are
  262. declaring macros:
  263.  
  264. #define SQUARE(x) x*x
  265.  
  266. The first string is replaced with the second one as normal,
  267. but every element (separated by commas if more than one) inside
  268. the parentheses also moved into the formula. A line like this:
  269.  
  270. nr = SQUARE(4);
  271.  
  272. Will be replaced like this:
  273.  
  274. nr = 4*4;
  275.  
  276.  
  277. It is however important to remember that the pre-processor
  278. consider everything to be strings, and a line like this:
  279.  
  280. nr = SQUARE(4+1);
  281.  
  282. Will be replaced like this:
  283.  
  284. nr = 4+1*4+1
  285.  
  286. Square of 4+1 is 25 (5*5), but nr is equal to 9. This shows
  287. that you have to be very careful when you define your macros.
  288. To solve this problem you only need to add some parentheses
  289. around the x:s:
  290.  
  291. #define SQUARE(x) (x)*(x)
  292.  
  293. nr = SQUARE(4+1);
  294.  
  295. Will then be replaced like this:
  296.  
  297. nr = (4+1)*(4+1);
  298.  
  299.  
  300. However, the macro is still not perfectly defined. Consider
  301. this statement:
  302.  
  303. nr = 16 / SQUARE(2);
  304.  
  305. Will then be replaced like this:
  306.  
  307. nr = 16 / (2)*(2);
  308.  
  309. 16 divided by square of 2 is 16/4, which is equal to 4, but
  310. nr is equal to 16 (16/2 * 2). The solution to all this problems
  311. is to put parentheses around every element, PLUS parentheses
  312. around the whole expression:
  313.  
  314. #define SQUARE(x) ((x)*(x))
  315.  
  316. nr = 16 / SQUARE(2);
  317.  
  318. Will then be replaced like this:
  319.  
  320. nr = 16 / ((2)*(2)); /* OK */
  321.  
  322.  
  323. What you need to remember about macros is that:
  324. 1. Put parentheses around every element.
  325. 2. Put also parentheses around the whole expression.
  326.  
  327.  
  328. Here is a list of some commonly used macros:
  329.  
  330. #define MAX(x,y)       ((x)>(y)?(x):(y))
  331. #define ABS(x)         ((x)<0?-(x):(x))
  332. #define ANSWER(c)      ((c)=='y'||(c)=='Y'?1:0)
  333.  
  334. Remember, if you have written some good constants/macros you
  335. can put them all in a header file and #include it every time you
  336. need them.
  337.  
  338.  
  339.  
  340. 0.3.3  OTHER PRE-PROCESSOR COMMANDS
  341.  
  342. #include and #define are the two most commonly used
  343. pre-processor commands, but there exist five more which can be
  344. used together with the #define command:
  345.  
  346. 1. You can check if something has been defined, and then define
  347.    something else. (#ifdef)
  348.  
  349. 2. You can check if something has not been defined, and then
  350.    define something else. (#ifndef)
  351.  
  352. 3. You can put an "else" statement which is connected to an
  353.    #ifdef/ifndef. (#else)
  354.  
  355. 4. You can put an end mark which is connected to an #ifdef/
  356.    #ifndef. (#endif)
  357.  
  358. 5. You can undefine the something that has already been defined.
  359.    (#undef)
  360.  
  361.  
  362. Here some examples which shows how you can use the pre-processor
  363. commands:
  364.  
  365. #define BIG 1
  366.  
  367. #ifdef BIG
  368.   #define HEIGHT 100  /* If BIG is defined, HEIGHT and WIDTH */
  369.   #define WIDTH  300  /* is defined to 100 and 300.          */
  370. #else
  371.   #define HEIGHT  50  /* If BIG is undefined, HEIGHT and     */
  372.   #define WIDTH   25  /* WIDTH is defined to 50 and 25.      */
  373. #endif
  374.  
  375.  
  376. These commands are very handy when you #include several files,
  377. and you want to check if something has been defined. If it is
  378. not defined you can include some other files and so on...
  379.  
  380. #ifndef MY_DEFINITIONS      /* If MY_DEFINITIONS is undefined */
  381.   #include "mydefinitions"  /* we include "mydefinitions".    */
  382. #endif
  383.  
  384.  
  385. The last pre-processor command, #undef, undefines the last
  386. definition:
  387.  
  388. #define HEIGHT 200  /* HEIGHT is defined as 200.            */
  389. #define HEIGHT 350  /* HEIGHT is redefined as 350.          */
  390. #undef HEIGHT       /* HEIGHT last definition is undefined, */
  391.                     /* and is redefined as previous to 200. */
  392. #undef HEIGHT       /* HEIGHT is now undefined.             */
  393.  
  394.  
  395.  
  396. 0.3.4  FUNCTIONS
  397.  
  398. C compilers expect in general to find function definitions
  399. (what type of data the function will return) before they are
  400. referenced. Since the main() function will come first, and
  401. will probably contain references to other functions further
  402. down, we need to declare the functions before the main()
  403. function. When a function is defined, before it is declared,
  404. we need to put a semicolon after it. The compiler will then
  405. understand that we only define a function, and that the
  406. function will be declared later on:
  407.  
  408. #include <stdio.h>
  409.  
  410. void main();             /* Will not return anything.     */
  411. int get_radius();        /* Will return an integer value. */
  412. float calculate_area();  /* Will return a float value.    */
  413. void print_area();       /* Will not return anything.     */
  414.  
  415. void main()
  416. {
  417.   int radius;
  418.   float area;
  419.   
  420.   radius = get_radius();
  421.   area = calculate_area( radius );
  422.   print_area( area );
  423. }
  424.  
  425. int get_radius()
  426. {
  427.   int r;
  428.   
  429.   printf( "Please enter the radius: " );
  430.   scanf( "%d", &r );  
  431.  
  432.   return( r );
  433. }
  434.  
  435. float calculate_area( r )
  436. int r;
  437. {
  438.   float a;
  439.  
  440.   a = 3.14 * r * r;
  441.  
  442.   return( a );
  443. }
  444.  
  445. void print_area( a )
  446. float a;
  447. {
  448.   printf( "The area is %f square units.\n", a );
  449. }
  450.  
  451.  
  452.  
  453. 0.3.5  VARIABLES
  454.  
  455. Here are the standard Lattice C data types:
  456.  
  457. -----------------------------------------------------------------
  458. |  TYPE            | BITS |   Minimum value  |   Maximum value  |
  459. |---------------------------------------------------------------|
  460. |  char            |   8  |            -128  |             127  |
  461. |  unsigned char   |   8  |               0  |             255  |
  462. |  short           |  16  |         -32 768  |          32 767  |
  463. |  unsigned short  |  16  |               0  |          65 535  |
  464. |  int             |  32  |  -2 147 483 648  |   2 147 483 647  |
  465. |  unsigned int    |  32  |               0  |   4 294 987 295  |
  466. |  long            |  32  |  -2 147 483 648  |   2 147 483 647  |
  467. |  unsigned long   |  32  |               0  |   4 294 987 295  |
  468. |  float           |  32  |         ±10E-37  |         ±10E+38  |
  469. |  double          |  64  |        ±10E-307  |        ±10E+308  |
  470. -----------------------------------------------------------------
  471.  
  472. Important, these data types can be different on other versions
  473. of C, so be careful when using them. To be sure that the size
  474. of the variables are the same on different systems there exist
  475. a special list of variable definitions. You only need to
  476. include the file "exec/types.h", and you have a list of safe
  477. variable types. The file defines, among many things, these
  478. commonly used data types: 
  479.  
  480. Amiga Data Types   Lattice C Data Types   Description
  481. -----------------------------------------------------------------
  482.  
  483. LONG               long                   Signed 32-bit
  484. ULONG              unsigned long          Unsigned 32-bit
  485. LONGBITS           unsigned long          32 bits manipulation
  486.  
  487. WORD               short                  Signed 16-bit
  488. UWORD              unsigned short         Unsigned 16-bit
  489. WORDBITS           unsigned short         16 bits manipulation
  490.  
  491. BYTE               char                   Signed 8-bit
  492. UBYTE              unsigned char          Unsigned 8-bit
  493. BYTEBITS           unsigned char          8 bits manipulation
  494.  
  495. VOID               void                   Nothing
  496. STRPTR             *unsigned char         String pointer
  497. CPTR               ULONG                  Absolute memory pointer
  498.  
  499. TEXT               unsigned char          Text
  500. BOOL               short                  Boolean (The file has
  501.                                           also defined the two
  502.                                           words TRUE = 1 and
  503.                                           FALSE = 0)
  504.                                           
  505. -----------------------------------------------------------------
  506. Here is a list of some data types which should not be used any
  507. more:
  508. -----------------------------------------------------------------
  509.  
  510. APTR               *STRPTR                Absolute memory pointer
  511.                                           (Misdefined, use CPTR!)
  512. SHORT              short                  Signed 16-bit   (WORD)
  513. USHORT             unsigned short         Unsigned 16-bit (UWORD)
  514.  
  515. -----------------------------------------------------------------
  516.  
  517. See file "exec/types.h" on the fourth Lattice C disk for more
  518. information. This file is automatically included when you
  519. include the "intuition.h" header file.
  520.  
  521.  
  522.  
  523. 0.3.6  STORAGE CLASSES FOR VARIABLES
  524.  
  525. There exist six main types of storage classes for variables:
  526.   1. Automatic
  527.   2. Formal
  528.   3. Global
  529.   4. External Static
  530.   5. Internal Static
  531.   6. Register
  532.  
  533.  
  534.  
  535. 0.3.6.1  AUTOMATIC
  536.  
  537. Automatic variables are declared inside functions with the
  538. keyword "auto" in front of them. (Variables which are declared
  539. inside a function with no keyword in front of them are assumed
  540. to also be automatic variables.) Space for them are reserved
  541. from the stack, and they can only be reached inside the same
  542. function that declared them. When the function has finished the
  543. memory is automatically deallocated back to the stack.
  544.  
  545. Here is a small recursion program. The user can enter values
  546. until he enters 0 when all the numbers will be printed out
  547. backwards.
  548.  
  549. #include <stdio.h>
  550.  
  551. main()
  552. {
  553.   get_number();
  554. }
  555.  
  556. get_number()
  557. {
  558.   auto int number; /* <== Here it is!             */
  559.                    /* Keyword "auto" is optional. */
  560.   
  561.   printf( "Enter a number (0: Quit) =>" );
  562.   scanf( "%d", &number );
  563.   if( number )
  564.     get_number();
  565.   else
  566.     printf( "%d\n", number );
  567. }
  568.  
  569.  
  570.  
  571. 0.3.6.2  FORMAL
  572.  
  573. Very similar to automatic variables. Memory is reserved from
  574. the stack, they can only be reached inside the function which
  575. declared them, the memory is automatically deallocated back to
  576. the stack when the function quits. The only difference is that
  577. they are the functions "parameters" and are declared after the
  578. parentheses, but before the curly brackets.
  579.  
  580. #include <stdio.h>
  581.  
  582. void main();
  583. void print_number();
  584.  
  585. void main()
  586. {
  587.   print_number( 3.142 );
  588. }
  589.  
  590. void print_number( number )
  591. float number;                /* <== Here it is! */
  592. {
  593.   printf( "The number is %f!\n", number );
  594. }
  595.  
  596.  
  597.  
  598. 0.3.6.3  GLOBAL
  599.  
  600. Global variables are declared outside the functions and can
  601. therefore be reached from everywhere. The memory is reserved
  602. in the BSS or DATA hunks, and can not be deallocated. (All
  603. uninitialized data is put in the BSS hunks, while all
  604. initialized data is put in the DATA hunks.)
  605.  
  606. int number;      /* Put in the BSS hunks.  */
  607. int width = 12;  /* Put in the DATA hunks. */
  608.  
  609. Information about global variables is put in the object file
  610. so other modules can reach the variables. If a variable is
  611. declared as a global variable (declared outside the functions)
  612. in module A, the variable should be declared as an external 
  613. global variable (declared outside the functions with the
  614. keyword "extern" in front of it) in module B.
  615.  
  616. --------------------------------------------------------------
  617. | Module A                                                   |
  618. --------------------------------------------------------------
  619. #include <stdio.h>
  620.  
  621. void main();
  622.  
  623. char name = "Andirs Bjerin"; /* Declare this variable as a  */
  624.                              /* global variable. (DATA)    */
  625. void main()
  626. {
  627.   print_name();
  628. }
  629.  
  630.  
  631. --------------------------------------------------------------
  632. | Module B                                                   |
  633. --------------------------------------------------------------
  634. #include <stdio.h>
  635.  
  636. void print_name();
  637.  
  638. extern char name;   /* Tell the compiler that this variable */
  639.                     /* actually exist, but is declared in   */
  640.                     /* another module.                      */
  641. void print_name()
  642. {
  643.   name[ 3 ] = 'e';
  644.   printf("Programmer: %s\n", name );
  645. }
  646.  
  647. To compile these two modules and link them you only need to:
  648.   1. Compile (lc1 and lc2) Module A: lc ModuleA.c
  649.   2. Compile (lc1 and lc2) Module B: lc ModuleB.c
  650.   3. Link the two object modules together with some other
  651.      libraries etc:
  652.      blink LIB:c.o, ModuleA.o, ModuleB.o TO program
  653.            LIB LIB:lc.lib, LIB:amiga.lib
  654.  
  655.  
  656.  
  657. 0.3.6.4  EXTERNAL STATIC
  658.  
  659. Same as global variables but can ONLY be reached inside the
  660. same module. All functions in that module will know about the
  661. variable, but no functions in other modules can reach it. The
  662. memory is taken from the BSS or DATA hunks, but no information
  663. about these variables is put in the object files.
  664.  
  665. To declare an external static variable you simply put the
  666. keyword "static" in front of a global variable.
  667.  
  668. static numbers[ 20 ];
  669.  
  670.  
  671.  
  672. 0.3.6.5  INTERNAL STATIC
  673.  
  674. Same as external static variables except that they are declared
  675. inside the functions, and can only be reached by that function.
  676. The special thing about internal static variables is that the
  677. memory is not deallocated when the the function has finished,
  678. and the values which were stored in them is untouched. (The
  679. memory was reserved from the BSS or DATA hunks.) An internal
  680. static variable will only be initialized once, but can later be
  681. changed, but not deallocated.
  682.  
  683. #include <stdio.h>
  684.  
  685. void main();
  686. void count_up();
  687.  
  688. void main()
  689. {
  690.   int loop;
  691.   
  692.   for( loop = 0; loop < 20; loop++ )
  693.     count_up();
  694. }
  695.  
  696. void count_up()
  697. {
  698.   static number = 1; /* We declare and initialize an internal */
  699.                      /* static variable. Since the number is  */
  700.                      /* a static variable it will only be     */
  701.                      /* initialized the first time we call    */
  702.                      /* the function. (DATA)                  */
  703.  
  704.   number++;
  705.   
  706.   printf("Number: %d\n", number );
  707. }
  708.  
  709.  
  710.  
  711. 0.3.6.6  REGISTER
  712.  
  713. Register variables are like automatic variables except that you
  714. ask the compiler if the variable can be allowed to use a
  715. processor register for it self. This is very handy if you use
  716. a variable a lot, for example in a loop. However, it is not
  717. sure that you can reserve a register. If you are lucky the
  718. compiler managed to reserve a register for the variable, but
  719. it is not always possible. The register variable will then be
  720. a normal automatic variable.
  721.  
  722. #include <stdio.h>
  723.  
  724. void main();
  725.  
  726. void main()
  727. {
  728.   register int loop; /* <== Here it is! */
  729.   
  730.   for( loop = 0; loop < 10000; loop++)
  731.     ;
  732. }
  733.  
  734.  
  735.  
  736. 0.3.7  POINTERS
  737.  
  738. Pointers are declared in the same way as normal variables with
  739. only one important difference. We put a "*" sign in front of
  740. the name:
  741.  
  742. int a;  /* "a" is an integer variable. */
  743. int *b  /* "b" is an integer pointer variable. */
  744.  
  745. If we want to get the address of a variable we put a "&" sign
  746. in front of the variable. "a" contains an integer value while
  747. "&a" is the memory address of that variable. If we want the
  748. pointer "b" to point to the variable "a", we simply write:
  749.  
  750. b = &a; /* "b" will contain the address of variable "a". */
  751.  
  752. It is important to remember that "b" is a pointer variable ("b"
  753. can point to any integer variable), while "&a" is a pointer
  754. constant ("&a" will always be the address of "a").
  755.  
  756.  
  757.  
  758. 0.3.8  STRUCTURES
  759.  
  760. I will not try to explain everything about structures, but I
  761. will however give a short summary of how to use them.
  762.  
  763.  
  764.  
  765. 0.3.8.1  HOW TO DECLARE STRUCTURES
  766.  
  767. You declare a structure type like this:
  768. 1. Write the keyword "struct" plus a name for the structure
  769.    type.
  770. 2. Put all variable-types with their names inside two curly
  771.    brackets.
  772.  
  773. If you want to declare a structure type called "person" which
  774. should consist of an integer field, named "age", and two float
  775. fields, named "height" and "weight", you write:
  776.  
  777. struct person
  778. {
  779.   int age;
  780.   float height, weight;
  781. }
  782.  
  783. You can put often used structures declarations in a header file
  784. and then include it when necessary. When we are going to
  785. program the Amiga we will use a lot of declared structures
  786. which mainly exist in the header file "intuition.h".
  787.  
  788.  
  789.  
  790. 0.3.8.2  HOW TO CHANGE THE STRUCTURE'S FIELDS
  791.  
  792. Once you have declared a structure type, you can declare
  793. structure variables and pointers. To declare a structure
  794. variable named "anders" and a structure pointer named
  795. "programmer" you write:
  796.  
  797. struct person anders;
  798.  
  799. struct person *programmer;
  800.  
  801. You can now initialize the structure variable "anders" with
  802. some data. You do it by writing the name of the structure
  803. variable, put a dot, and then write the field you want to
  804. access. So if you want to change the age in the structure
  805. variable "anders" to 20, the height to 1.92, and the weight
  806. to 74.5 you write:
  807.  
  808. anders.age = 20;
  809. anders.height = 1.92;
  810. anders.weight = 74.5;
  811.  
  812.  
  813.  
  814. 0.3.8.3  POINTERS AND STRUCTURES
  815.  
  816. If we want to initialize the pointer "programmer" so it points
  817. to the "anders" structure, we write:
  818.  
  819. programmer = &anders;
  820.  
  821. Remember: "anders" is the structure variable.
  822.           "&anders" is the address of the structure variable.
  823.  
  824. Once the structure pointer points to a structure we can start
  825. to check and change values of that structure. The important
  826. thing to remember is that we do not put a dot between the
  827. pointer name and the field name, we put a "->" sign. (The
  828. pointer "programmer" points to the field "height":
  829. programmer -> height etc.)
  830.  
  831. if( programmer -> age == 20 )
  832.   /* The programmer is 20 years old. */
  833.  
  834. programmer -> weight -= 5; /* The programmer is on a diet. */
  835.  
  836.  
  837.  
  838. 0.3.9  CASTING
  839.  
  840. If you want to convert one type of data into another type of
  841. data you need to use a method which is usually referred as
  842. "casting". If you want to convert a WORD variable into a LONG
  843. variable type you would write something like this:
  844.  
  845. WORD number;
  846. LONG data;
  847.  
  848. data = (LONG) number;
  849.  
  850. The command (LONG) tells the C compiler that the next data type
  851. should be converted into a LONG variable type.
  852.  
  853. When you convert different variable types you normally do not
  854. need to do any casting. In the previous you could therefore
  855. have left out the (LONG) command, but to make it clear for
  856. anyone who reads the code that it is no mistake, it is best to
  857. keep it. 
  858.  
  859. However, when you convert different pointer types you should
  860. use the casting method. Since the most common errors in C have
  861. something to do with pointers, C compiler are usually very fussy
  862. about which pointers may point to which variables/structures
  863. etc.
  864.  
  865. If we have declared a pointer to be a normal memory CPTR
  866. pointer (programmer), and want it to point to a structure
  867. (person), the compiler would be very confused.
  868.  
  869. struct person anders;
  870. CPTR programmer;
  871.  
  872. programmer = &anders;
  873.  
  874. A memory pointer, and a pointer to a structure, is actually the
  875. same thing, but the paranoid compiler does not believe it, and
  876. will give us a warning which look something like this:
  877.  
  878. Example.c 27 Warning 30: pointers do not point to same type of object
  879.  
  880.  
  881. To tell the compiler that the two different types of memory
  882. addresses are actually the same thing you need to do some
  883. casting. We need to tell the compiler that a pointer to the
  884. structure anders (&anders) is actually a memory pointer (CPTR).
  885.  
  886. programmer = (CPTR) &anders;
  887.  
  888.  
  889.  
  890. 0.4 LIBRARIES
  891.  
  892. Many special functions have already been written for you, and
  893. are placed in different "libraries". If you want to access any
  894. of the functions, you simply need to "open" the right library.
  895. You open a library by calling the function OpenLibrary(), and
  896. it will return a pointer to a library structure.
  897.  
  898. There exist roughly two different types of libraries, ROM
  899. libraries which are placed in ROM (surprise, surprise), and
  900. Disk libraries which are placed on the boot disk (system disk).
  901.  
  902.  
  903.  
  904. 0.4.1  ROM LIBRARIES
  905.  
  906. ROM libraries are always available (they are placed in the ROM)
  907. but they still need to be opened before you may use their
  908. functions. Here is a list of the ROM Libraries:
  909.  
  910. GRAPHICS   This library contains all functions which has
  911.            anything to do with graphics: Display (View,
  912.            ViewPorts and RastPorts), sprites (Hardware/
  913.            software sprites and BOBs), text and fonts etc.
  914.            Chapter 10 and some examples in the chapters 1 - 9
  915.            uses this library.
  916.  
  917. LAYERS     This library contains routines that handles the
  918.            screen in such a way that different "layers"
  919.            (planes) can be used and overlap each other.
  920.             
  921. INTUITION  This library takes care of the "user interface" and
  922.            works with the Graphics and Layers libraries. Takes
  923.            care of screens/windows/gadgets/requesters/menus
  924.            etc. Chapter 1 - 9 explains how to use this library.
  925.  
  926. EXEC       Takes care of the memory/tasks/libraries/devices/
  927.            resources, i/o ports etc.
  928.  
  929. DOS        AmigaDOS. Contains routines which open/close/read/
  930.            write files etc. 
  931.  
  932. MATHFFP    Motorola Fast Floating Point Routines. Handles
  933.            addition, subtraction, division, multiplication,
  934.            comparison, conversion etc. (Single Precision)
  935.  
  936. RAM-LIB    Takes care of the RAM disk.
  937.  
  938. EXPANSION  Handles the expansion slots.
  939.  
  940.  
  941.  
  942. 0.4.2  DISK LIBRARIES
  943.  
  944. If a programs opens a disk library, AmigaDOS will look in the
  945. logical device "LIBS:", which has been assigned to system
  946. disk's "libs" directory, and will then load the whole library
  947. into RAM. If the library has already been loaded into RAM by
  948. another task, Exec will then use the already existing library.
  949. (This shows how memory efficient libraries are, since several
  950. program can use the same code.)
  951.  
  952. Here is a list of the Disk Libraries:
  953.  
  954. TRANSLATOR       This library translates English strings into
  955.                  phonetic strings.
  956.  
  957. DISKFONT         Handles the Fonts on the disk.
  958.  
  959. ICON             Takes care of the "icons" used by the
  960.                  Workbench.
  961.  
  962. MATHTRANS        Contains functions like sin, cos, log, ln,
  963.                  square, power etc.
  964.  
  965. MATHIEEEDOUBBAS  Same as the ROM Library "MATNFFP" but uses
  966.                  Double Precision.
  967.  
  968.  
  969.  
  970. 0.4.3  OPEN AND CLOSE LIBRARIES
  971.  
  972. You open, as said above, a library by calling the function
  973. OpenLibrary(). It will return a pointer to a library structure,
  974. and the pointer MUST be stored in a pointer variable with a
  975. special name. For example, if you open the Intuition library,
  976. the pointer should be called "IntuitionBase", and if you open
  977. the Graphics library, the pointer should be called "GfxBase".)
  978.  
  979. Synopsis: pointer = OpenLibrary( name, version );
  980.  
  981. pointer:  (struct Library *) Pointer to a library structure.
  982.           If you open the Intuition library it is a pointer
  983.           to an IntuitionBase structure, and should be called
  984.           "IntuitionBase". If you open the Graphics library
  985.           it is a pointer to a GfxBase structure, and should
  986.           be called "GfxBase". (You will need to do some
  987.           casting here.)
  988.  
  989. name:     (char *) Pointer to a string containing the library
  990.           name. The Intuition library is called 
  991.           "intuition.library", and the Graphics library is
  992.           called "graphics.library".
  993.  
  994. version:  (long) You can here specify that the user need a
  995.           special version (or higher) of the library to run
  996.           the program. If you want any version of the
  997.           library, simply write 0. (The highest library
  998.           version is for the moment 34.)
  999.  
  1000.  
  1001. Every library which has been opened, must be closed before the
  1002. program terminates. You close a library by calling the function
  1003. CloseLibrary(), with the library pointer as the only argument.
  1004. The ROM libraries do not actually need to be closed since they
  1005. exist in ROM, and will not occupy any valuable memory, but
  1006. close it anyway. (Always follow the rule: Close everything that
  1007. you have opened!)
  1008.  
  1009. Synopsis: CloseLibrary( pointer );
  1010.  
  1011. pointer:  (struct Library *) Pointer to a library structure
  1012.           which has been previously initialized by an
  1013.           OpenLibrary() call.
  1014.  
  1015.  
  1016. In this manual we will only discuss functions in the Intuition
  1017. and Graphics libraries. Here is a fragment of a program that
  1018. opens and closes the Intuition library:
  1019.  
  1020. struct IntuitionBase *IntuitionBase;
  1021.  
  1022. main()
  1023. {
  1024.   /* Open the Intuition Library (any version): */
  1025.   IntuitionBase = (struct IntuitionBase *)
  1026.     OpenLibrary( "intuition.library", 0 );
  1027.   
  1028.   if( IntuitionBase == NULL )
  1029.     exit(); /* Could NOT open the Intuition Library! */
  1030.  
  1031.  
  1032.   ... ...
  1033.  
  1034.  
  1035.   /* Close the Intuition Library: */
  1036.   CloseLibrary( IntuitionBase );
  1037. }
  1038.  
  1039.  
  1040. Here is another fragment of a program which this time opens and
  1041. closes the Graphics library:
  1042.  
  1043. struct GfxBase *GfxBase;
  1044.  
  1045. main()
  1046. {
  1047.   /* Open the Graphics Library (any version): */
  1048.   GfxBase = (struct GfxBase *)
  1049.     OpenLibrary( "graphics.library", 0 );
  1050.   
  1051.   if( GfxBase == NULL )
  1052.     exit(); /* Could NOT open the Graphics Library! */
  1053.  
  1054.  
  1055.   ... ...
  1056.  
  1057.  
  1058.   /* Close the Graphics Library: */
  1059.   CloseLibrary( GfxBase );
  1060. }
  1061.  
  1062.  
  1063.  
  1064. 0.5  MEMORY
  1065.  
  1066. The Amiga can be expanded to a total of 9MB of memory. This
  1067. memory can be divided into three different types:
  1068.  
  1069. Chip Memory  The first 512KB is normally called "Chip Memory",
  1070.              and can be accessed both by the processor as well
  1071.              as the co-processors and the other special chips
  1072.              in the Amiga. (All graphics/Sound etc which is
  1073.              used by the special Chips (Blitter etc) must be
  1074.              located here!)
  1075.  
  1076. Slow Memory  The following 512KB (extra memory which is put
  1077.              inside the Amiga) can only be accessed by the main
  1078.              CPU, and can therefore not be used to store
  1079.              graphics and sound data. It is called "Slow" since
  1080.              it is not accessible by the Chips, but can be
  1081.              interrupted by them. If you have a screen with a
  1082.              high resolution and/or many colours this memory
  1083.              will be slowed down since the Chips will steal
  1084.              some memory cycles from it.
  1085.              
  1086.              There exist a new Chip set which enables the
  1087.              co-processors and Chips to use this memory also,
  1088.              which means that this memory could be used to
  1089.              store graphics/sound data etc. However, most of
  1090.              the Amigas (99.9%) have still the old Chips, and
  1091.              this memory can NOT be used for graphics/sound
  1092.              data.
  1093.  
  1094. Fast Memory: The third type of memory is the following 8MB,
  1095.              which can ONLY be accessed by the main processor,
  1096.              and is NOT slowed down by the Chips. This memory
  1097.              is therefore called "Fast Memory".
  1098.  
  1099.  
  1100. The important thing to remember is that all Graphics and Sound
  1101. data (all data handled by the Chips) MUST be stored in the
  1102. "Chip Memory"! There exist three different ways of putting the
  1103. data into the Chip Memory:
  1104.  
  1105. 1. If you use the Lattice C Compiler V5.00 or higher, you can
  1106.    put the keyword "chip" in front of the name of the data, and
  1107.    the Compiler will automatically put it in the Chip Memory.
  1108.    Eg. UWORD chip graphics_data[] = { ... };
  1109.  
  1110. 2. If you use the Lattice C Compiler V4.00 you can compile the
  1111.    source code with the added command "-acdb" which will load
  1112.    all initialized data (DATA) into Chip Memory when the
  1113.    program is loaded.
  1114.    Eg. lc -acdb -Lm Example.c
  1115.  
  1116. 3. Allocate some Chip Memory by calling the function AllocMem(),
  1117.    and then move all data into the newly allocated memory.
  1118.  
  1119.  
  1120. There exist other methods, such as running the program "ATOM"
  1121. etc, but I will not explain it here. (See your own manuals for
  1122. more information.)  
  1123.