- 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:
- 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.
- 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
- 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
- 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:
- 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