C Language -- pointer, -- pointer
I. pointer 1. pointer declaration format
1> Format: variable type * variable name. For example, char * p defines a pointer Variable p, which can only store addresses.
* Remember * it's not a pointer variable name, it's just a pointer sign
2> role: the pointer acts as a storage address and accesses the corresponding bucket based on the address value.
3> example
1 int main () 2 {3 int a = 90; 4 int * p; 5 p = & a; 6 return 0; 7}Pointer code
* Representation of code in memory
* Note: ① int * p can only point to int type data. ② the pointer variable is initialized. Do not use it to indirectly access its bucket. ③ write like this is not allowed: * p = & a, * p indicates the bucket pointed to by the Access pointer p. ④ the definition variable * is just a symbol and has no special meaning.
2. pointer to pointer 1. Example
Char a = 'a ';
Char * p = & a; // pointer to a char-type Bucket
Char ** pp = & p; // pointer to pointer
Char *** ppp = & pp; // Level 3 pointer (not commonly used)
2. pointer Application
1> call a function to obtain multiple return values
1 # include <stdio. h> 2 3 int sumAndMinus (int num1, int num2, int * num3) 4 {5 * num3 = num1-num2; 6 return num1 + num2; 7} 8 9 10 int main () 11 {12 int a = 12; 13 int B = 8; 14 int minus; 15 int sum = sumAndMinus (a, B, & minus); 16 return 0; 17}Multiple return values 3. Memory occupied by pointers
* Pointer memory usage is related to the specific compiler. In Xcode, pointers of any type occupy 8 bytes.
3. pointer and array 1. array Traversal
// Use subscript for Traversal
Int nums [3] = {1, 2, 3 ,}
For (int I = 0; I <3; I ++)
{
Printf ("% d \ n", nums [I]);
}
2. traverse the Array Using pointers
Int nums [3] = {1, 2, 3 ,};
Int * p = & nums;
// Pointer p points to the address of the first element of the array;
// (P + 1) points to nums [1];
For (int I = 0; I <3; I ++)
{
Printf ("% d \ n", * (p + I ));
Printf ("% d \ n", p [I]);
}
4. pointer and string, pointer array 1. character array and string comparison
Char c1 [] = "name ";
// The above line of code indicates a character array, which represents a string variable
Char * c2 = "name ";
// The above line of code. The pointer variable c2 points to the first letter of the string. It indicates a String constant.
// The differences between the two lines of code are also related to the storage of data in the memory.
Char * c3 = "name ";
// Note: c2 and c3 are the same address.
2. Two Methods for defining strings
1> Use Arrays
Char c [] = "name ";
// Features: the characters in the string can be modified.
// Usage: the string content needs to be modified frequently.
2> Use Pointer
Char * c = "name ";
// Feature: the character string is actually a constant string, and the characters in it cannot be modified.
// Usage: the content of the string does not need to be modified, and this string is often used.
3. pointer Array
1> pointer array Definition
Char * name [3] = {"jack", "rose", "tom "};
2> string Input
// Create a new character array to save the string entered by the user
Char name [20];
// The array name indicates the address of the first element of the array.
Scanf ("% s", name );
5. pointer to the function and function that returns the pointer 1. function that returns the pointer
Char * test ()
{
Return "name ";
}
Int main ()
{
Char * p = test ();
Return 0;
}
2. pointer to function
Void test ()
{
Printf ("test function called ");
}
Int main ()
{
// (* P) is a fixed writing method, which indicates that the pointer Variable p will definitely point to the function in the future.
// Void on the left: no return value is returned for the function pointed to by the pointer Variable P.
// () On the Right: The pointer Variable p points to the function with no form parameters
Void (* p )();
// The function name is the function address.
P = test;
// Function call Methods
(* P) (); // Method 1: directly call
Test (); // Method 2: Use Pointer to call
P (); // method 3: Use Pointer to call
Return 0;
}