home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / 2014.11.minnie.tuhs.org.tar / minnie.tuhs.org / UnixArchive / PDP-11 / Trees / V6 / usr / doc / ctut / ct6 < prev    next >
Text File  |  1975-06-27  |  1KB  |  54 lines

  1. .NH
  2. Initialization of Variables
  3. .PP
  4. An external variable may be initialized at compile time
  5. by following its name with an initializing value
  6. when it is defined.
  7. The initializing value has to be something whose value is known at compile time,
  8. like a constant.
  9. .E1
  10. int    x    0;    /\** "0" could be any constant \**/
  11. int    a    'a';
  12. char    flag    0177;
  13. int    \**p    &y[1];    /\** p now points to y[1] \**/
  14. .E2
  15. An external array can be initialized by following its name with
  16. a list of initializations enclosed in braces:
  17. .E1
  18. int    x[4]    {0,1,2,3};        /\** makes x[i] = i \**/
  19. int    y[ ]    {0,1,2,3};        /\** makes y big enough for 4 values \**/
  20. char    \**msg    "syntax error\\n";    /\** braces unnecessary here \**/
  21. char \**keyword[ ]{
  22.     "if",
  23.     "else",
  24.     "for",
  25.     "while",
  26.     "break",
  27.     "continue",
  28.     0
  29. };
  30. .E2
  31. This last one is very useful _
  32. it makes 
  33. .UL keyword
  34. an array of pointers to character strings,
  35. with a zero at the end so we can identify the
  36. last element easily.
  37. A simple lookup routine could scan this until
  38. it either finds a match or encounters a zero keyword pointer:
  39. .E1
  40. lookup(str)        /\** search for str in keyword[ ] \**/
  41.    char \**str; {
  42.     int i,j,r;
  43.     for( i=0; keyword[i] != 0; i\*+) {
  44.         for( j=0; (r=keyword[i][j]) \*= str[j] && r != '\\0'; j\*+ );
  45.         if( r \*= str[j] )
  46.             return(i);
  47.     }
  48.     return(-1);
  49. }
  50. .E2
  51. .PP
  52. Sorry _
  53. neither local variables nor structures can be initialized.
  54.