C Pointers and Arrays Notes

已有 2807 次阅读 2014-4-19 15:15 |个人分类:C语言|系统分类:科研笔记| C语言, 数组, 指针, 多维指针



1) The type void *(pointer to void) replaces char * as tge proper type for a generic pointer.

2)The & operator only applies to objectin memory: variables and array elements. It cannot be applied to expressions, constants, or register variables.

3)If a pointer p points to a particular element of an array, then by definition p+1 points to the next of element, p+i points i elements after p, and p-i points i elements before. These remarks are ture regardless of type of size of the variables in the array a (even the variables are essentially arrays?)

4) a[i] is equivalent to *(a+i), and is therefore equivalent to i[a].

5)There is one difference between an array name and a pointer. A pointer is a variable, so p++ is legal. but an array name is not a variable, so a++ is illegal.

6)As formal parameters in a function definition, char s[] and char *s are equivalent. but we prefer the latter because it says more explicitly that the variable is a pointer.但在普通声明中则不同。前者s是字符数组名,后者是字符型指针。

7)It is possible to pass part of an array to a function, by passing a pointer to the beginning of the subarray. For example, if a is an array,f(&a[2]) and f(a+2) both pass to the function f the address of the subarray that starts at a[2].

8)Pointers and integers are not interchangeable. Zero is the sole exception: the constant zero may be assigned to a pointer, and a pointer may to compared to the constant zero. The symbolic constant NULL is often used in place of zero.

9)Any pointer can be meaningfully compared for equality or inequality with zero. But the behavior is undefined for arithmetic or comparisons with pointers that do not pointer to members of the same array. (There is one exception: the address of the first element past the end of an array can be used in pointer arithmetic.)

10)p+n: n is scaled according to the size of the object p points to, which is determined by the declaration of p. Pointers subtraction is also valid, if p and q point to elements of the same array, and p < q, then q-p is the number of elements from p to q inclusive.

11)The valid pointer operation are assignment of pointers of the same type, adding or subtracting a pinter and an integer, subtracting of comparing two pointers to members of the same array, and assigning or comparing to zero. All other pointer arithmetic is illegal.

12)C does not provide any operators for processing an entire string of characters as a unit. There is an important difference between these definitions:
char p[] = "This is a string";
char *p = "This is a string";

13)If a two-dimensional array is to be passed to a function, the parameter declaration in the function must include the number of columns; More generally, only the first dimension subscript of an array is free; all the others have to be specified.

14)Compare int a[10][20] and int *b[10]. They are different. The important advantage of the

pointer array is that the rows of the array may be of different length.



【1】 Brian W. Kernighan & Dennis M. Ritchie. The C Programming Language (Second Edition)



该博文允许注册用户评论 请点击登录 评论 (0 个评论)


Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2021-2-25 05:14

Powered by

Copyright © 2007- 中国科学报社