First, pointers and constants
Constants are not directly amplitude to the pointer, if you want to give the pointer amplitude constant, you need to do the following:
int cal=8;
int *pcal=&cal;
Pcal points to a 8 stored address, *pcal the data stored for that address 8.
Second, pointer and array
Pointers and arrays have a natural connection, in fact the array is a continuous address holds the constant, we look at the following example:
int arry[3]={1,3,5};
Then Arry is the first address of the array, and *arry is the data 1,* (arry+1) that holds the first address of the array, and the data stored in the second position of the array 3
As can be seen from the above:
*arry and Arry[1] also point to the first element of the array.
Further extended, as follows:
A, int *data[3] is an array of pointers, each element in the array is a pointer to the int data, and is assigned the following values:
int arry[3]={1,3,5};
int *data[3]={arry,arry+1,arry+2};
Data[0] to point to Arry first address, *data[0] data stored for this address 1. Data is the first address of the data array, *data and Data[0] are the first addresses of the arrays Arry. In fact, it can be said that three arrays of data[0], data[1] and data[2] are defined.
b, Int (*data) [3] is an array pointer, an array, each element to hold an address, assigned the following values:
int arry[3]={1,3,5};
Int (*data) [3]=&arry;
Then *data is the address of the first element, and **data is the element 1 where the first address is located.
Third, the pointer and the structure body
struct person
{
Char *pername;
int age;
}person;
If you want to give the *pername amplitude, you can do the following:
Person.pername= "Jack";//Use the pointer to complete the assignment
If the struct is defined as
struct person
{
Char pername[20];
int age;
}person;
Person.pername[20]= "Jack" is not possible, the reason is that the structure is an abstract data type, do not allocate storage units, so the array assignment does not pass. If you use pointers to assign values, you can solve such problems.
Iv. Pointers and functions
Pointers to functions are common in Linux operating systems
int print (int a, int b, char *p)
{printf ("in Put number sum was%d,input string is%s\n", a+b,p);//three parameters, int int and char*
return a+b;}
int main ()
{
Int (* pprint) (Int,int,char *);//define pointer function *pprint to function print, three arguments, int int and char*
pprint=print;//assigning values to pointer functions
int C;
C=pprint (3,6,var);
printf ("%d\n", c);//output print return value
}
A function is actually a special function program that starts from an address, whose function name is the first address of the program, so you can assign a value to the pointer function: pprint=print;//assigns a value to the pointer function
This article from "Big Plum" blog, declined reprint!
C Language Pointers