- A bit-field or field for short, is a set of adjacent bits within a single implementation-defined storage unit that we will call a "word"
Friday, June 17, 2011
6.9 Bit-fields
6.8 Unions
- A union is a variable that may hold (at different times) objects of different types and sizes, with the compiler keeping track of size and alignment requirements
- Unions provide a way to manipulate different kinds of data in a single area of storage, without embedding any machine-dependent information in the program
- Syntax:
Thursday, June 16, 2011
6.7 Typedef
- C provides a facility called typedef for creating new data type names
- ie. typedef int Length; (Length a synonym for int)
- ie. typedef char *String; (String a synonym for char * or character pointer)
Wednesday, June 15, 2011
Tuesday, June 14, 2011
6.3 Arrays of Structures
- Structure type key, with an array keytab of structures. Each elements of the array is a structure:
- Initialization:
Monday, June 13, 2011
6.2 Structures and Functions
- The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with & and accessing its member
- Copy and assignment including passing arguments to functions and returning values from function as well
- Structure may not be compared
- A structure may be initialized by a list of constant member values; an automatic structure may also be initialized by an assignment
Thursday, June 9, 2011
6.1 Basics of Structures
- Structure example:
- An optional name called a structure tag may follow the word struct. (ie point)
- The variables named in a structure are called members
- A struct declaration defines a type
- A struct declaration that is not followed by a list of variables reserves no storage, it merely describes a template or the shape of a structure.
- If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure
- ie: struct point pt; defines a variable pt which is a structure of type struct point
- A member of a particular structure is referred to in an expression by a construction of the form: structure-name.member (ie. point.x )
- Structures can be nested:
Wednesday, June 8, 2011
Chapter 6 Structures
- A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling
- Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities
- ANSI standard: Structures may be copied and assigned to, passed to functions, and returned by functions.
Tuesday, June 7, 2011
5.12 Complicated Declarations
- C is sometimes castigated for the syntax of its declarations, particularly ones that involve pointers to functions
- The syntax is an attempt to make the declaration and the use agree; it works well for simple cases, but it can be confusing for the harder ones, because declarations cannot be read from left to right, and because parentheses are over-used.
- Problem:
- * is a prefix operator and it has lower precedence than ( ), so parentheses are necessary to enforce proper association
Monday, June 6, 2011
5.11 Pointers to Functions
- In C, a function itself is not a variable, but it is possible to define pointers to functions, which can be assigned, placed in arrays, passed to functions, returned by functions, and so on
Friday, June 3, 2011
5.10 Command-Line Arguments
- In environments that support C, there is a way to pass command-line arguments or parameters to a program when it begins executing.
- When main is called, it is called with two arguments or parameters.
- The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (argv for argument vector) is a pointer to an array of character strings that contain the arguments, one per string.
- The simplest illustration is the program echo, which echoes its command-line arguments on a single line, separated by blanks:
- By convention, argv[0] is the name by which the program was invoked, so argc is at least 1
- If argc is 1, there are no command-line arguments after the program name
- In the example above, argc is 3; argv[0] is "echo", argv[1] is "hello," and argv[2] is "world"
- Standard requires that argv[argc] be a null pointer:
- First version of echo treats argv as an array of character pointers. Since argv is a pointer to an array of pointers, we can manipulate the pointer rather than index the array:
- Second version is based on incrementing argv, which is a pointer to pointer to char, while argc is counted down:
Thursday, June 2, 2011
5.9 Pointers vs Multi-dimensional Arrays
- Compare the declaration and picture for an array of pointers:
- Important advantage of the pointer arrays is that the rows of the array may be of different lengths
Wednesday, June 1, 2011
Subscribe to:
Posts (Atom)