- 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
Thursday, May 26, 2011
5.7 Multi-dimensional Arrays
- C provides rectangular multi-dimensional arrays, in practice they are much less used than array of pointers
- In C, a two dimensional array is really a one dimensional array, each of whose elements is an array. Hence subscripts are written as:
- An array is initialized by a list of initializers in braces
- If a two-dimensional array is to be passed to a function, the parameter declaration in the function must include the number of columns; the number of rows is irrelevant
Wednesday, May 25, 2011
5.6 Pointer Arrays; Pointers to Pointers
- Pointers are variables and can be stored in arrays
- Sort a set of text lines in alphabet order:
- Each line of character array can be accessed by a pointer to its first character and pointers themselves can be stored in an array
- When two out-of-order lines have to be exchanged, the pointers in the pointer array are exchanged, not the text lines themselves
- These prevent complicated storage management and high overhead of moving the lines themselves
Tuesday, May 24, 2011
5.5 Character Pointers and Functions
- Strcpy ( ) function:
- strcmp ( ) function:
- *--p decrements p before fetching the character that p points to
- Standard idioms for pushing and popping stack:
Monday, May 23, 2011
5.4 Address Arithmetic
- C's integration of pointers, arrays and address arithmetic is one of the strengths of the language
- alloc(n), returns a pointer p to n-consecutive character positions, which can be used by the caller of alloc for storing character
- afree(n) releases the storage thus acquired so it can be re-used later
- Storage managed by alloc and afree is a stack or last-in, first-out list
- In general, a pointer can be initialized to zero or an expression involving the addresses of previously defined data of appropriate type
- C guarantees that zero is never a valid address of data, so a return value of zero signal an abnormal event
- Pointers and integers are not interchangeable
- Zero is sole exception: the constant zero may be assigned to a pointer, and a pointer may be compared with the constant zero
- The construction p+n means the address of the n-th object beyond the one p currently points to
- n is scaled according to the size of the objects p points to, which is determined by the declaration of p.
- The valid pointer operations are assignment of pointers of the same type, adding or subtracting a pointer and an integer, subtracting or comparing two pointers to members of the same array, and assigning or comparing to zero
Thursday, May 19, 2011
5.3 Pointers and Arrays
- For pa = &a[0]
- a[i] can also be written as *(a+i); &a[i] = a+i
- A pointer is a variable, so pa = a and pa++ is legal
- An array name is not a variable, so a = pa and a++ is illegal
- When an array name is passed to a function, what is passed is the location of the initial element
- Within the called function, this argument is a local variable, and so an array name parameter is a pointer(variable containing an address)
- As formal parameters in a function definition, char s[] and char *s are equivalent
- It is possible to pass part of an array to a function, by passing a pointer to the beginning of the subarray
Subscribe to:
Posts (Atom)




















