Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1015496
  • 博文数量: 327
  • 博客积分: 9995
  • 博客等级: 中将
  • 技术积分: 4319
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-25 11:21
文章存档

2011年(31)

2010年(139)

2009年(157)

我的朋友

分类: C/C++

2009-06-05 21:33:02

P94
The & operator only applies to objects in memory: variables and arrays elements. It cannot be applied to expressions,constant, or register variables.
P94
You should also note the implication that pointer is constrained to point to a particular kind of object: every pointer points to a specific data type.
(There is one exception: a "pointer to void" is used to hold any type of pointer but cannot be dereferenced(被解除参照) itself.)
P95
(*ip)++
The parentheses are necessary in this example.
because unary operators like * and ++ associate right to left.
SEE also P53 Table Precedence and Associativity of operators
! ~ ++ -- +(positive) -(minus) * & (type) sizeof  associativity: right to left
P99
There is one difference between an array and a pointer that must be kept in mind.
A pointer is a variable, so pa=a and pa++ are legal.
But an array name is not a variable; construction like a=pa and a++ are illegal.
P99
As format parameters in a function definition,
char s[]
and
char *s
are equivalent;

P101
This array is private fo alloc and afree. Since they deal in pointers, not array indicies, no other routine need know the name of the array,which can be declared static in the source file containing alloc and afree, and thus be invisible outside it.
In practical implementations, the array may well not even have a name,it might instead be obtained by calling malloc or by asking the operating system for a pointer to some unnamed block storage.

P102
Pointers and integers are not interchangeable.
Zero is the sole exception:the constant zero may be assigned to a pointer, and a pointer may be compared with the constant zero.
The symbolic constant NULL is often used to in place of zero, as a mnemonic to indicate more clearly that this is a special value for a pointer.
NULL is defined in .
P102
several important facts of pointer arithmetic.
First, pointers may be compared under certain circumstances,If p and q point to members of the same array, then reltions like == != < >=,etc,woke properly.
Any pointer can be meaningfully compared for equality or inequality with zero.
But the behaivor is undefined for arithmetic or comparisons with pointers that do not point 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).     ??
Second,we have already observed that a pointer and an integer may be added or substracted.
Pointer substraction is also valid.
P103
size_t is the unsigned integer type returned by the sizeof operator.
P103
All the pointer manipulatios automatically take into account the size of the object pointed to.
详见下面这个例子:
main()
{
   int a[5]={1,2,3,4,5};
   int *ptr=(int *)(&a+1);
   printf("%d,%d",*(a+1),*(ptr-1));
}
输出:2,5
*(a+1)就是a[1],*(ptr-1)就是a[4]
&a+1不是首地址+1,系统会认为加一个a数组的偏移,是偏移了一个数组的大小(本例是5个int)
int *ptr=(int *)(&a+1);
则ptr实际是&(a[5]),也就是a+5
  
原因如下:
&a是数组指针,其类型为 int (*)[5];
而指针加1要根据指针类型加上一定的值,不同类型的指针+1之后增加的大小不同。
a是长度为5的int数组指针,所以要加 5*sizeof(int)
所以ptr实际是a[5]
但是prt与(&a+1)类型是不一样的(这点很重要)
所以prt-1只会减去sizeof(int*)
a,&a的地址是一样的,但意思不一样
a是数组首地址,也就是a[0]的地址,&a是对象(数组)首地址
a+1是数组下一元素的地址,即a[1],&a+1是下一个对象的地址,即a[5]
P104
There is an important difference between these difinition:
char message[] = "now is the time";
char *pmessage = "now is the time";
message is an array, just big enough to hold the sequence of characters and '\0' that initializes it. Individual charaters within the array may be changed but message will always refer to the same storage.
On the other hand, pmessage is a pointer,initialized to point to a string constant.the pointer may be subsequently be modified to point elsewhere, but the result is undefined if you try to modify the string contents.
P112
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 row is irrelevant, since what is passed is ,as before, a pointer to an array of of rows, where each row is an array of 13 ints.In this paricular case, it is  a pointer to objects that are arrays of 13 ints.
阅读(579) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~