The previous type identifier indicates the type of the variable pointed to by the pointer variable, and can only point to the initialization of this type of variable pointerInt a = 10; int * p = & a; float B = 2.3f; float * q; q = & B;
The pointer operator assigns a value to the variable pointed to by the pointer.Char a = 10;
Printf ("value of a before modification: % d \ n", );
// Pointer Variable p points to variable
Char * p = & a; // This * specifies the pointer description.
// Indirectly modify the value of variable a through the pointer Variable p
* P = 9; // This * is a pointer operator that assigns 9 to the address a pointed to by the pointer, which is equivalent to a = 9;
// Here, the value of a is indirectly modified.
Printf ("value of a after modification: % d", );
Obtains the value of the variable pointed to by the pointer.Char a = 10;
Char * p;
P = &;
Char value = * p; // access the corresponding bucket based on the p value (that is, the address of variable a), obtain the stored content (that is, retrieve the value of variable a), and assign the value to the value.
Printf ("Get a value: % d", value );
Usage notesDo not assign values to the content indicated by the pointer variable before it points to a fixed address. The following statements are incorrect.
Int * p;
* P = 10; // This is incorrect.
Assign a value after the pointer variable points to a fixed variable. The following statements are correct.
// Define two int Variables
Int a = 6, B;
// Define a pointer Variable p pointing to variable B
Int * p;
P = & B;
// Assign the value of a to variable B
* P =;
ExampleSwap the address of two character variables (change the value of the real parameter)
Void swap (char * p, char * q)
{
Char temp = * p;
* P = * q;
* Q = temp;
}
Int main (int argc, constchar * argv [])
{
Char a = 'A', B = '&';
Swap (& a, & B );
Printf ("a = % c B = % c \ n", a, B );
}
Point an element to a one-dimensional array with a pointerInt a [2] = {2, 3}; int * p = & a [0]; * p = 10; then a [0] should be equal to 10
The address of array name a is the same as the address of its first element, so p = & a [0] has the same effect as p =
Pointers to traverse array elements
Int ary [] = {1, 2, 4, 5 };
Int * q = ary;
For (int I = 0; I <5; I ++)
{
// The Element Memory Address in the array is a continuous storage method. When the pointer moves a corresponding type in bytes (int, char...), it points to the next element value.
// Printf ("number: % d", * (q + I); // move the address
// Printf ("number: % d", * (ary + I); // move the address
Printf ("number: % d", * (q ++); // q = q + 1, move the pointer to the address
// Printf ("number: % d", * (ary ++); // The constant cannot be assigned a value.
}
Array, pointer, function parameterParameter array, real parameter pointer
Void change (int B []) {
B [0] = 10;
}
Int main ()
{
// Define an array of the int type
Int a [4] = {1, 2, 3, 4 };
Int * p =;
// Pass array name a to the change function
Change (p );
// View a [0]
Printf ("a [0] = % d", a [0]);
Return 0;
}
Parameter pointer, real parameter Array
Void change (int * B ){
B [0] = 10;
// Or * B = 10;
B [1] = 11;
// Or * (B + 1) = 11;
}
Int main ()
{
// Define an array of the int type
Int a [4] = {1, 2, 3, 4 };
// Pass array name a to the change function
Change ();
// View a [0]
Printf ("a [0] = % d", a [0]);
Return 0;
} // You can see that in many cases, pointers and arrays can be switched to each other. However, pointers are not equal to arrays.
Use a pointer to traverse all characters in a stringChar chs [] = "abcde ";
Char * p;
P = chs;
For (; * p! = '\ 0'; p ++)
{
Printf ("data: % c", * p );
}
Printf ("\ n ");
Direct pointer to stringChar * p = "abcde ";
Strlen ("abde ");
Function declaration in string. hSize_t strlen (const char *);
Char * strcpy (char *, const char *); // string copy Function
Char * strcat (char *, const char *); // String concatenation Function
Int strcmp (constchar *, constchar *); // string comparison Function
Their parameters are pointer types pointing to character variables, so you can pass in Pointer variables or array names.
Other methods for pointing a pointer to a string1 char s [10];
2 s = "mj"; // the compiler must report the error of Row 3 because s is a constant and represents the first address of the array. Value assignment is not allowed.
1 char * s = "mj ";
2
3 * s = "like ";
Two errors were made in the 3rd line of code: