Pointer
Review the knowledge of pointers
Pointer Category
Pointer point the constant:
1
const char *name = "chen" //statement a pointer point a constant
because using
const,so the Pointer can't change variable in the address which it point ,so the statement as follows is incorrect :1
name[3]='a' //incorrect,pointer "name" can't change constant
but name is a normal pointer ,so it could change the items it point,statement as follows are correct:
1
name = 'zhang' //change the address the pointer point ,correct
Also,Even you have changed your string you point ,you still can't change the string, Please somebody tell me why ,Thank you !
1
name[3]='y' //incorrect,but I don't know why!
Constant Pointer
A pointer can't change the address it point ,but it still can change the content it point,example:
1
2
3char *const name ="chen"; //define a constant pointer
name[3] = 'a'; //correct ,it can change the constent of pointer
name = "zhang"; //incorrect ,it can't change the address it pointsConstant Pointer points to constant
A constant pointer points a constant ,the address pointer point is unchangeable and the content of address is unchangeable,example :
1
2
3const char *const name="chen"; //define a constant pointer point the constant
name[3] = 'q'; //incorrect ,the constant is unchangeable
name ='ninglang'; //incorrect ,the address pointed is unchangeableConst
Using a const to define a
integervariable ,the keyword omitted is acceptable the definition as following is same:1
2const int LIMITS = 100;
const LIMITS = 100;formal parameters also can be describe by
const,for example:1
int MAX(const int*ptr)
the method promise the array can't be changed ,only be read.
Pointer array
1 |
|
in fact , you see the definition of Pointer array ,It is like as follows:
1 | char *arr[3]={'abc','def','ghi'}; |
1 | char *pChar1 = 'abc',*pChar2 = 'def',*pChar3='ghi' |
At the same time :
1 | arr[0] = pChar; //the arr first element is the pointer pChar |
and the pChar is pointing the 'abc''s first element 'a', so we can use the code to print 'a'
1 | printf("%c",pChar[0]); //print 'a' |
Pointer Function
1 | int fun(int x,int y); //normal function return integers |
This function declaration is normal ,but There are some difference in next function declaration
1 | int *fun(int x,int y) |
This function declaration is pointer function ,the return is a pointer to int ,This is an address
Pointer To Function
To state a pointer to function ,which is a Pointer pointing function .declaration form:
1 | int (*fun)(int x,int y) |
There are two ways to assign values to pointer variables
1 | fun = &function; |
There are also two ways to call pointer to function
1 | x=(*fun)(); |
Example:
1 |
|
New and Delete
operation new can get a space from heap and return the pointer to point the first address of the memory,and delete can free the space
1 | int *p; |
new assign space for multidimensional array:
1 | int i = 3; |
new assign space with initial value:
1 |
|