define
- How to define a pointer variable
/* Datatype * variable name Datatype: indicates what type of variable address a pointer variable can hold; * : has no special meaning, just to indicate that this is a pointer variable */
int num = 10;
int * p;
p = & num; // Pointer variables can only hold addresses
//int * p = #
printf("num = %d \n",num);//num = 10
printf("p = %p \n", p);//p = 0x7ffeefbfef1c
printf("&num = %p \n", &num);//&num = 0x7ffeefbfef1c
printf("*p = %d \n",*p);//*p = 10
//*p: access the memory space pointed to by p
Copy the code
Basic data types
The argument to the function isValue passed
To modify the parameter value in a functionDon't
Affects the value of the outside argumentAn array of
The argument to the function isaddress
If it isaddress
Modifies the value of a parameter in a functionwill
Affects the value of the outside argument
Pointer point
- Pointers can only hold addresses
- The same variable can have multiple Pointers to it
- The pointing of a pointer can be modified
- Do not access wild Pointers
- What type of pointer can only access what type of data
Pointers and Arrays
- The name of the array is the address of the array == the address of the first element of the array
int ages[3] = {1.3 , 5};
printf("ages = %p\n", ages);//ages = 0x7ffeefbfef0c
printf("&ages[0] = %p\n", &ages[0]);//&ages[0] = 0x7ffeefbfef0c
Copy the code
And since the address of the first element of the array is the address of the array, and we define a pointer to the array, we actually define a pointer to the first element of the array, so what type is the first element of the array, and what type is the pointer to the array
int * p1 = &ages[0];
int * p2 = ages;
printf("p1 = %p\n", p1);//p1 = 0x7ffeefbfef0c
printf("p2 = %p\n", p2);//p2 = 0x7ffeefbfef0c
Copy the code
- Pointer operated array
// Pointers operate on arrays
p1[2] = 90;
// ages = {1, 3 , 90};
Copy the code
- Traverse the array through the pointer + 1
- Pointer +1, not really +1, but +4(4 bytes),
- Two uses for Pointers
- When the value is set, the corresponding bytes of data are extracted according to the bytes occupied by the pointer type
- For addition purposes, the pointer +1 is actually adding the length of the pointer type. If the current pointer type is int, then +1 is essentially adding 4 bytes
printf("p1+0 = %d\n", *(p1 + 0));/ / 1
printf("p1+1 = %d\n", *(p1 + 1));/ / 2
printf("p1+2 = %d\n", *(p1 + 2));/ / 90
Copy the code
Pointers and Strings
- The difference between arrays holding strings and Pointers holding strings
- Array holds strings, strings are one
variable
strYou can modify
- Pointer holds a string, string is a
constant
strDo not modify the
- Array holds strings stored in
The stack
; The pointer saves the string saved inThe constant area
- Variables stored on the stack have a feature that is automatically released when the scope ends
- Values stored in a constant area have the property that they are not released and that multiple identical values correspond to the same address
- Array holds strings, strings are one
// Save the string
char str[] = "wcl";
printf("str = %s\n", str);//wcl
str[0] = 'm';
printf("str = %s\n", str);//mcl
// The array name is the address of the array. The array name holds the address of the 0th element of the array, so we can use Pointers to hold strings
// A pointer to a string is the address of the 0th element of the string
char * str1 = "wcl";
printf("str1 == %p\n",str1);//0x100003f50
// str1[1] = 'j'; //Thread 1: EXC_BAD_ACCESS
printf("*str1 = %c\n", *str1);//w
printf("*str1 = %c\n", *(str1 + 1));//c
printf("*str1 = %c\n", *(str1 + 2));//l
printf("*str1 + 0_p = %p\n", &(*str1));//0x100003f50
printf("*str1 + 1_p = %p\n", &(*(str1 + 1)));//0x100003f51
printf("*str1 + 2_p = %p\n", &(*(str1 + 2)));//0x100003f52
printf("sizeof(*str1) = %ld\n".sizeof(*str1));/ / 1
printf("sizeof(str1) = %ld\n".sizeof(str1));/ / 8
char * str2 = "wcl";
printf("str2 == %p\n",str2);//0x100003f50
Copy the code
Exercise: Custom methods to calculate the length of a string
int myStrlen(char *str) {
int count = 0;
while(*str ! ='\ 0') {
printf("str_p == %p \n",str);
/** str_p == 0x100003f9e str_p == 0x100003f9f str_p == 0x100003fa0 str_p == 0x100003fa1 str_p == 0x100003fa2 str_p == 0x100003fa3 */
str = str + 1;
count ++ ;
}
/** \0 ASCII 0 // 0 represents false 1 true while (* string ++)count++; * /
return count;
}
Copy the code
A function pointer
- Functions take up a chunk of storage, so they have their own address
- The address of the function is stored in the function name, which is the address of the function (the array name is the address of the array).
How a function pointer is defined
return_Type (*functionP)(arg_type); * : represents a pointer; FunctionP: variable name for pointer; (*function) : indicates a future reference to a function; Return_Type: indicates the return value type of the pointed function. (arg_type) : indicates the type of function argument to point to;Copy the code
void test(a)
{
printf("Test executed \n");
/* void (*functionP)(); * : represents a pointer; FunctionP: variable name for pointer; (*function) : indicates a future reference to a function; Void: indicates that the pointing function has no return value. () : indicates that the pointing function has no argument */
}
int getAge(a)
{
return 20;
}
void sum(int v1, int v2)
{
int res = v1 + v2;
printf("res = %i\n", res);
}
int sum2(int v1 , int v2)
{
int res = v1 + v2;
return res;
}
int main(int argc, const char * argv[]) {
printf("test_p = %p\n",test);//0x100003f10
// No return value, no arguments
void(*test_P)(void);
//void(*test_P)(void) = test;
test_P = test;
test_P();
// Return value, no arguments
int (*ageP)();
ageP = getAge;
printf("age = %i\n", ageP());
// There are arguments, no return values
void (*sumP)(int.int);
sumP = sum;
sumP(10.20);
// There are return values and arguments
int (*sumP2)(int , int);
sumP2 = sum2;
printf("sum = %i\n", sumP2(10.20));
return 0;
}
Copy the code
Function as argument
int sum(int v1, int v2)
{
return v1 + v2;
}
int minus(int v1, int v2)
{
return v1 - v2;
}
/ / / the action method
/// @param v1 V1
/// @param v2 V2
// @param function function pointer
int action(int v1,int v2,int(*function)(int.int)) {
int result = function(v1,v2);
return result;
}
int main(int argc, const char * argv[]) {
int(*function_1)(int.int) = sum;
int(*function_2)(int.int) = minus;
// int sum_v = action(10, 20, function_1);
// int minus_v = action(20, 10, function_2);
int sum_v = action(10.20, sum);
int minus_v = action(20.10, minus);
printf("sum_v = %d,\nminus_v = %d\n",sum_v,minus_v);
return 0;
}
Copy the code