LEC20081107 - Introduction to Computer Programming November...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Computer Programming November 7, 2008 CSC180 Fall 2008, University of Toronto Arrays of structures We can declare an array of structures. Example: #define NKEYS 10 struct key { char *word; int count; } keytab[NKEYS]; The above structure declaration declares a structure type key , defines an array keytab of structures, and sets aside storage for keytab . CSC180 Fall 2008, University of Toronto 1 Another way: #define NKEYS 10 struct key { char *word; int count; }; struct key keytab[NKEYS]; Structure initialization: /* from PuTTY */ typedef enum { CS_NONE, /* used for reporting errors, etc */ CS_ISO8859_1, CS_ISO8859_1_X11, /* X font encoding with VT100 glyphs */ CS_ISO8859_2, CS_ISO8859_3, CSC180 Fall 2008, University of Toronto 2 CS_ISO8859_4 } charset_t; struct { const char *name; charset_t type; int return_in_enum; /* enumeration misses some charsets */ } localencs = { { "<UNKNOWN>", CS_NONE, 0 }, { "ISO-8859-1", CS_ISO8859_1, 1 }, { "ISO-8859-1 with X11 line drawing", CS_ISO8859_1_X11, 0 }, { "ISO-8859-2", CS_ISO8859_2, 1 }, { "ISO-8859-3", CS_ISO8859_3, 1 }, { "ISO-8859-4", CS_ISO8859_4, 1 } }; Exercise: read and understand the following program. /* keyword.c */ #include <ctype.h> #include <stdio.h> CSC180 Fall 2008, University of Toronto 3 #include <string.h> #include "getch.h" /* can be found in LEC20081103.pdf */ struct key { char *word; int count; } keytab = { /* keywords sorted alphabetically */ { "auto", 0 }, { "break", 0 }, { "case", 0 }, { "char", 0 }, { "const", 0 }, { "contine", 0 }, { "default", 0 }, { "unsigned", 0 }, { "void", 0 }, { "volatile", 0 }, { "while", 0 } }; #define NKEYS (sizeof keytab / sizeof keytab[0]) #define MAXWORD 100 int getword(char *, int); int binsearch(char *, struct key *, int); CSC180 Fall 2008, University of Toronto 4 /* count C keywords */ int main() { int n; char word[MAXWORD]; while (getword(word, MAXWORD) != EOF) if (isalpha(word[0])) if ((n = binsearch(word, keytab, NKEYS)) >= 0) keytab[n].count++; for (n = 0; n < NKEYS; n++) if (keytab[n].count > 0) printf("%4d %s\n", keytab[n].count, keytab[n].word); return 0; } /* get next word or character from standard input */...
View Full Document

This note was uploaded on 01/10/2011 for the course CSC 180 taught by Professor Na during the Fall '01 term at University of Toronto- Toronto.

Page1 / 23

LEC20081107 - Introduction to Computer Programming November...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online