The C Programming Language 笔记(2)

Chapter 5: Pointers and Arrays

 

 

    Pointers are much used in C, partly because they are sometimes the only way to express a computation, and partly they usually lead to more compact and efficient code than can be obtained in other ways.
    The unary operator & gives the address of an object
     
    THe & cann't be applied to expressions, constants, or register variables.
    Pointers can deference anything, but themselves.
   
    notice:unary operator like * and ++ associate right to left. so:
    ++*p and (*p)++ and *p += 1 are equal.
   
Pointers and Function arguments:
    Since C passed agruments to functions by value, there is no direct way for the called function to alter a variable in the calling function. So, it isn't enough to write
         
    
    The way to obtain the desired effect is:
         
    and change the formal of function's arguments to
         
    
Pointers and Arrays:
     
    
    There is one difference bewteen an array name and a pointer name that must be kept in mind. A poiner is a variable, so pi = a and pi++ are legal. But an array name is not a variable; constructions like a = pi and a++ are illegal.
   
Address arithmetic:
    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. All other pointer arithmetic is illegal. It isn't legal to add two pointers, or to multiply or divide or shift or mask them, or to add float or doule to them, or even, except for void *, to assign a pointer to one type to a pointer of another type without a cast.
   
Character pointers and Functions :
    C doesn't provide any operators for processing an entire string of characters as a unit.
   
    notice:     
   
Pointers to Functions:
    In C, a function itself isn't a variable, but it's possible to define pointers to functions, which can be assigned, placed in array, passed to functions, returned by functions, and so on.
     notice:
    * is a prefix operator and it has lower precedence than (), soparentheses are necessary to force the proper association.    
   
         
   
Chapter 6: Structures

 

    A structure is a collection of one or more variables, possibly of different types, grouped under a single name for converient handling.
    The main change made by the ANSI standard is to define structure assignment-structure may be copied and assigned to, passed to functions, and returned by functions. Automatic structures and arrays may now are initialized.
     
    
    A sizeof can't be used in a #if line, because the preprocessor doesn't parse type names. But the expression in the #define is not evaluated by the preprocessor, so the code here is legal.
     
   
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.
    In effect, a union is a structure in which all members have offset zero from the base, the structure is big enought to hold the "widest" member, and the alignment is appropriate for all of the types in the union. The same operations are premitted on unions as on structures: assignment to or copying as a unit, taking the address, and accessing a member.
    A union may only be initialized with a value of the type of its first number.
   
Bit-fields:
     
    
    Almost everything about fields is implementation-dependent.
    They are not arrays, and they don't have address, so the & operator can't be applied to them.

   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值