一、结构基础-2013-3-30

一、结构
1、结构是一个或者多个变量的集合,这些变量可以是不同的类型。某些语言将结构称为“记录”    。
2、结构可以拷贝、赋值、取地址、传递给函数, 函数也可以返回结构类型的返回值。

3、结构的声明由关键词struct 引入,由包含在花括号内的一系列声明组成; 关键词后面struct后面的名字是可选的,
     称为标记。 (如果无结构名,而且也没有typedef声明,是没有意思的,不能定义其instance)。
4、结构成员、结构标记、普通变量 可以采用相同的名字,他们不会引起冲突。
5、如果结构声明的后面不带变量表,则不需要为他们分配存储空间,它仅仅描述了一个结构的模板或轮廓。
 
二、结构数组
1、结构数组的长度 = sizeof  数组名  / sizeof 数组的一个元素。 sizeof的返回值是无符号整型,其类型为size_t,该
     类型在头文件<stddef.h>中定义。
 
2、条件编译语句#if中不能使用 sizeof,因为预处理器不对类型名进行分析。 但是,在#define 中使用sizeof 是合法
     的,因为预处理器并不计算#define语句中的表达式。
 
三、指向结构的指针
1、&tab[-1]和&tab[n]是非法的。(经过验证,能编译并运行,只是没有意义!。) C语言的定义保证数组末尾之后
     的第一个元素(即&tab[n])的指针运算可以正确执行。
 
2、如果p是指向结构的指针,则对p的算术运算需要考虑结构的长度,所以,表达式p++的执行时,将在p的基础上加
    上一个正确的值,以确保得到结构数组的下一个元素。
 
3、结构的长度不一定等于各成员长度的和,因为不同的对象有不同的对齐要求。所以结构中会出现未命名的hole 。

四、自引用结构
a、二叉树
  1、任何节点最多拥有两个子树,也可能只有一个子树或者一个都木有。
  2、二叉树是递归定义的。所以它的操作也是基于递归实现的. 它的基本操作:init  destroy        addt  delete  traverse
  3、一个包含自身实例的结构是非法的,可是一个包含自身引用的结构是合法的。
 
b、hash表
  1、The algorithm is a hash-search - the incoming name is converted into a small non-negative
     integer, which is then used to index into an array of pointers.

  2、An array element points to the beginning of a linked list of blocks describing names that have that hash value.  
      It is NULL if no names have hashed to that value.
     元素结构定义如下:
     struct nlist { /* table entry: */
        struct nlist *next; /* next entry in chain */
       char *name; /* defined name */
       char *defn; /* replacement text */
     };

    hash table定义如下:
    #define HASHSIZE 101
    static struct nlist *hashtab[HASHSIZE]; /* pointer table */

   3、The hashing function, adds each character value in the string to a scrambled combination of the previous
       ones and returns the remainder modulo the array size. This is not the best possible hash function, but it is short
       and  effective.

  4、要保证哈希函数的返回值非负。它为hash table数组的索引,即为元素在哈希表的入口。链表是一个单向列表,
       最后一个元素的next为 null.
 
五、Typedef
a、C provides a facility called typedef for creating new data type names. For example, the declaration typedef int
Length; makes the name Length a synonym for int. The type Length can be used in declarations。

b、typedef并木有定义新类型,只是一个类型的同义词。It must be emphasized that a typedef declaration does not
     create a new type in any sense; itmerely adds a new name for some existing type.
 
c、真的很奇怪,这样的代码也能编译并运行。
    typedef struct aabb *Treeptr;
    int main(int arg, char **argv)
    {
         Treeptr pt;
         pt = (Treeptr)33;
         printf("%lu\n",(unsigned long)pt);
    }

六、Unions
a、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。They    are analogous   to variant records in pascal.
 
b、This is the purpose of a union - a single variable that can legitimately hold any  of one  of several types。
 
c、The syntax is based on structures:
    union u_tag {
          int ival;
         float fval;
        char *sval;
      } u;
   The variable u will be large enough to hold the largest of the three types;  the specific size is implementation-
   dependent.
 
d、Syntactically, members of a union are accessed as  union-name.member  or   union-pointer->member
 
e、The notation for accessing a member of a union in a structure (or vice versa) is identical to that for nested
     structures
 
f、In effect, a union is a structure in which all members have offset zero from the base. The same operations are
    permitted on unions as on structures。include:
   
printf("the addr of the u is %lu\n", (unsigned long)&u);
    printf("the addr of the u'member is %lu\n", (unsigned long)&u.fval);

 
七、Bit-fields
a、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.''
 
b、definition
        struct {
             unsigned int is_keyword : 1;
             unsigned int is_extern : 1;
             unsigned int is_static : 1;
       } flags;
 
c、Individual fields are referenced in the same way as other structure members:  flags.is_keyword,
    flags.is_extern, etc.
d、Almost everything about fields is implementation-dependent. Whether a field may overlap a word boundary is
     implementation-defined.
e、Fields may be declared only as ints; for portability, specify signed or unsigned explicitly.
f、They are not arrays and they donot haveaddresses, so the & operator cannot be  applied on them.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值