1. Pointers
A pointer (Pointer) is an object in a programming language that uses an address whose value points directly (points to) where the value of another place in the computer's memory is present;
The definition format for pointers:
Data type * variable name;
Attention:
1. When the definition determines the type of the pointer variable, the pointer can only hold the address of the corresponding type variable.
int number = 10;int *pointer; Defines an int type pointer pointer = &number;//The pointer variable stores the address of the number variable, and we think the pointer pointer points to the number
2. The pointer can only store addresses.
3. The same variable can have multiple pointers pointing to him
int number = 10;int *pointerone = &number;int *pointertwo = &number;//has two pointers pointing to the number variable
4. The pointer can change the variable pointed to
int Numberone = 10;int Numbertwo = -1;int *pointer = &numberone;//pointer points to numberoneint *pointer = &numberTwo; The pointer points to the Numbertwo
5 cannot access the wild pointer (pointer not pointing to be a wild pointer)
2. String pointers
In C, the string has two forms to store the string, 1. stored by an array. 2. Pass the pointer
The difference between the two:
1. The string stored by the array, which is a variable stored in the memory stack, can be modified.
2. A string pointer, which is a constant, stored in a constant area, cannot be modified.
3. Array of pointers
An array to store pointers to
Definition Format: Data type * array name [array length]
Char *str1 = "AAA"; Char *str2 = "BBB"; Char *STR3 = "CCC"; Char *array[] = {str1, str2, STR3}; for (int i = 0; i < 3; i + +) { printf ("%s\n", Array[i]); }
4. Pointers to functions
Definition Format: Return value type (* pointer variable name) (function argument list);
Attention:
1. The function is also allocated storage space, the function name holds the address of the function, similar to the array.
Functions cannot be defined in 2.C languages, so we can use function pointers to invoke different functions inside the function
#import <foundation/foundation.h>void Upchar (char *charvalue); void Lowerchar (char *charvalue); void Changechar ( Char str[],void (* style) (char *)); int main (int argc, const char * argv[]) { //capitalize the input string to lowercase, lowercase to uppercase //1. Prompt user for input printf ("Please enter a paragraph in English"); Char str[100] = {0}; Gets (str); This method is unsafe Changechar (Str,upchar);//Uppercase Changechar (str, LOWERCHAR);//lowercase printf ("%s", str); return 0;} Change the string void Changechar (char str[],void (* style) (char *)) { char *pointer = str; while (*pointer! = '))//Remove each letter of the string { style (pointer); pointer++; Increment the address within the pointer by the number of bytes in the data type }}//lowercase void Upchar (char *charvalue) { if (*charvalue >= ' a ' && * Charvalue <= ' z ') { *charvalue = *charvalue-(' A '-' a ');} } uppercase void Lowerchar (char *charvalue) { if (*charvalue >= ' A ' && *charvalue <= ' Z ') { * Charvalue = *charvalue + (' A '-' a ');} }
Pointer to C language