Monday, January 25, 2010

The declaration and algorithm about Pointers

I.Declare a pointer

int* p;     p is an integer pointer variable
int* p[n];   p is a pointer array by the amount of n-pointer pointing to an integer elements
int* p();   p points to a function which return an integer pointer
int (*p)();   p is a function pointer, points to a function which return an integer value
int** p;    p is an pointer variable points to another pointer, this pointer point to an integer

II. Algorithm
(1)get address:

use "&" get the address of the variable

(2)get value:

use "*" get the target of the pointer

(3)assign operate:

·assign variable address to an pointer
int i;
int* p = &i;
·between the same type pointer variable assignment
int* p = &i;
int * q = p;
·address of the first element in the array assign to pointer
int a[3]={10,20,30};
int* p = a;
·assigned to the function entry address of a pointer variable
typedef int (*funcptr)();
funcptr p;
extern int foo();
p = &foo;

(4)Addition and Subtract

·array pointer
int a[5]={10,20,30,40,50};
int* p = a;
p+=3; p-=3; p++; P--;
·subtract a pointer from a pointer of the same type,
get the difference in the subscripts of the two elements. So, the resulting type isn't a pointer but is a signed integer type.

(5)Comparison

·The two pointers point to the same array of variables can be greater than, less than, equal to the comparison operator.
·Compared with Zero,p==0 means point to NULL。

1 comment: