C pointer Elementary, pointer elementary
1:
/*
Variables must be stored in the computer;
*/
Int main (int argc, const char * argv []) {int a = 10; // four bytes on the stack; // & get the address character, & a indicates the address printf of a ("the address of a is % p \ n", & a); // print the address of address a of a is: 0x7fff5fbff81c // * Indirect addressing character printf ("the value of a's address is % d \ n", * (& )); // print the value of a. The value on the address of a is 10 return 0;} <span style = "color: # e32400;"> </span>
2:
/*
Pointer: a variable that stores the address of a variable;
Pointer definition: int * p; // * indicates the pointer. p indicates the pointer name, int indicates the data type to which the pointer is directed, or p Stores the address of an int variable;
*/
Int main () {int * p; // * only the pointer identifies int a = 10; int B = 20; p = &; // p can save an int data address; a is an int, so p can save the address of a; if p saves the address of a, p points to; printf ("p = % p, & a = % p \ n", p, & a); printf ("* p = % d \ n", * p ); // content pointed to by p; * p = 100; // the content on the address is modified through the address; * The indirect addressing character is used to retrieve the content in the p address; printf ("a = % d \ n", a); p = & B; // The value of p has changed; p points to data changed; p points to another data; * p = 200; // modified the content pointed to. B is modified; printf ("a = % d, B = % d \ n", a, B ); return 0 ;}
Int main () {int a = 10; int * p = & a; // int * p; * indicates the pointer ID, and int * indicates the p type; equivalent to int * p; p = & a; printf ("% d \ n", * p); int * q; // defines a pointer variable, if the pointer is not directed to any value, * the q operation may be abnormal. You must assign a value to the pointer and then use it (to tell the pointer to which variable) // * q = 100; printf ("* q = % d \ n", * q); return 0 ;}
Int main () {int * p; // char * q; // other pointer types // long * w; printf ("sizeof (p) = % lu \ n ", sizeof (p); // sizeof (p) = 8 printf (" sizeof (int *) = % lu \ n ", sizeof (int *); // sizeof (int *) = 8 printf ("sizeof (char *) = % lu \ n", sizeof (char *)); // sizeof (char *) = 8 printf ("sizeof (long *) = % lu \ n", sizeof (long *); // sizeof (long *) = 8 printf ("sizeof (double *) = % lu \ n", sizeof (double *); // sizeof (double *) = 8 // All pointer types occupy the same memory in the memory, which is 8 bytes in 64-bit systems; return 0 ;}
3:
/*
Pointer operation ++, --, + ,-
Address calculation;
*/
Int main () {int a [] = {1, 2, 3, 4, 5}; // a is the array name, which stores the address of the first element of the array, that is, a = & a [0]; int * p; // p can store the address of the int type data; a contains five int, and p can point to any element in it; // p points to the first element a [0]? P = & a [0]; // equivalent to p = a; printf ("p = % p \ n & a [0] = % p \ na = % p ", p, & a [0], a); // p = 0x7fff5fbff810 & a [0] = 0x7fff5fbff810 a = 0x7fff5fbff810 p ++; // p offsets an int backward, which is 4 bytes = the size of the Data Type pointed to by p; printf ("\ np = % p \ n * p = % d \ n", p, * p ); // p points to a [1] p = 0x7fff5fbff814 * p = 2 p = a; for (int I = 0; I <5; I ++) // 1 2 3 4 5 {printf ("% d", * p); p ++;} p = & a [4]; for (int I = 0; I <5; I ++) // 5 4 3 2 1 {printf ("% d", * p --);} return 0 ;}
Int main () {int a [] = {1, 2, 3, 4, 5}; int sum = 0; int * p = a; p ++; // p points to a [1]; printf ("% lu \ n", p-a); // address 1-address 2: it indicates several data types that can be pointed to between two addresses, and several int values; 1 printf ("% d \ n", * (p-1); // address-one number, it is an address, that is, the pointer is offset N x N data types pointing to; 1 p = a; // The array is traversed through p + I; p + I = & a [I], * (p + I) = a [I]; for (int I = 0; I <5; I ++) // 1 2 3 4 5 {printf ("% d", * (p + I);} p = & a [4]; for (int I = 0; I <5; I ++) // 5 4 3 2 1 {printf ("% d", * (p-I);} p =; for (int I = 0; I <5; I ++) {sum + = * (p + I);} printf ("\ nsum = % d \ n ", sum); // sum = 15 return 0 ;}
4:
/*
Wild pointer
*/
Int main () {int * p; // It is defined only. If no value is assigned, the pointer is arbitrary, that is, a wild pointer. printf ("p = % p \ n", p ); * p = 100; // modified the uncertain items. This operation is forbidden. A low-level error occurs. return 0 ;}
5:
/*
NULL Pointer: if the value of p is NULL, p is a NULL pointer;
NULL is an Invalid Address. * p cannot be read or written;
The pointer must be empty before use;
*/
Int main () {int * p = NULL; printf ("p = % p", p); // printf ("* p = % d \ n ", * p); // error, program error if (p = NULL) {printf ("p is a NULL pointer and cannot use \ n ");} else {printf ("p valid \ n ");}}
6:
/*
Void * pointer, wildcard pointer, can point to any data type, but pay attention to force conversion to point to the type during use;
*/
Int main () {void * p; char ch = 'a'; int I = 1234; double B = 12.38; printf ("sizeof (p) = % lu \ n ", sizeof (p); // sizeof (p) = 8 p = & ch; // p points to the char type variable // printf ("% c \ n ", * p); // compilation failed, Type mismatch; printf ("% c \ n", * (char *) p); // (char *) is type description; a p = & I; printf ("% d \ n", * (int *) p); // 1234 p = & B; printf ("% lf \ n ", * (double *) p); // 12.380000 return 0 ;}
Void swap (int a, int B) // It is passed only as a parameter. When the function ends, it is destroyed. The purpose of the switch cannot be reached. {int temp = a; a = B; B = temp ;} void swap_true (int * a, int * B) // The address is passed. The operation is stored in the address, and can be exchanged. {int temp = *; * a = * B; * B = temp;} int main () {int x = 10, y = 20; swap (x, y ); printf ("x = % d, y = % d \ n", x, y); // x = 10, y = 20 swap_true (& x, & y ); printf ("x = % d, y = % d \ n", x, y); // x = 20, y = 10 return 0 ;}
7:
/*
Array and pointer
When an array is passed as an input parameter of a function
Both the form parameter and the actual parameter can be in the pointer or array form;
*/
Void setArray (int a [], int len) // The form parameter is in the array format {// printf ("sizeof (a) = % lu \ n ", sizeof (a); // The result is 8. a is a pointer and 8 bytes. Only the first address of the array can be passed. The number of elements cannot be passed for (int I = 0; I <len; I ++) {a [I] = 100 ;}} void printArray (int a [], int len) // The format parameter is in the array format {for (int I = 0; I <len; I ++) {printf ("% d", a [I]);} void setArray1 (int * a, int len) // The form parameter is in the pointer format {// a [0] = * a & a [0] = a [I] = * (a + I) for (int I = 0; I <len; I ++) {* (a + I) = 10 ;}} int main () {int a [5] = {1, 2, 3, 4, 5}; // a is the array name, saving the first address of the array; array name a can be considered as a constant pointer; int * p = a; // p can only point to one int, pointing to one element, not an array; here p points to the first address of the array setArray (a, 5 ); // The actual parameter format is printArray (p, 5); // The actual parameter format is setArray1 (p, 5); printArray (p, 5 ); printf ("% d \ n", * (a + 2); return 0 ;}
8:
/*
Summary of arrays and pointers
1: When the array is passed as the input parameter of the Function
Both the form parameter and the actual parameter can be in the pointer or array form;
When the parameters are arrays, they are actually pointer variables;
SetArray (int a [], int len)
In this example, a is a pointer and cannot be used to calculate the number of elements in the meta array.
In this example, a is a variable and can be assigned a value. a ++ is allowed;
2: The array name can be regarded as a constant pointer (a constant pointer pointing to the first element); the address of the first element of the array is saved;
Int a [5];
In this case, a cannot be assigned a value, and a ++ is incorrect;
A [I] = * (a + I) = * (p + I)
*/
// Use the pointer to pass the array and sort void sortArray (int * p, int len) // sort {for (int I = 0; I <len-1; I ++) {for (int j = 0; j <len-i-1; j ++) {if (* (p + j)> * (p + j + 1 )) {int temp = * (p + j); * (p + j) = * (p + j + 1); * (p + j + 1) = temp ;}}} void printArray (int * p, int len) // print {for (int I = 0; I <len; I ++) {printf ("% d", * (p + I) ;}} int printMax (int * p, int len) // find the maximum value {int max = * p; // define the maximum value as the first element of the array for (int I = 0; I <len; I ++) {if (max <* (p + I )) {max = * (p + I) ;}} return max ;}int main () {int a [] = }; int len = sizeof (a)/sizeof (a [0]); sortArray (a, len); printArray (a, len ); printf ("\ nthe max is % d \ n", printMax (a, len); return 0 ;}
9:
/*
Character array and pointer
*/
/*
Differences between constant strings and character arrays;
Constant string cannot be changed;
The character array can be changed. The character array must have its own space;
*/
Int main () {char str [100] = "helloworld"; // assign a value to the variable character array; char * p = "helloworld"; // p points to the constant string, * p cannot be used to modify the constant string. p points to 'H' in the constant string. // * p = 'H'; // cannot be modified. p points to the constant area and is read-only, not writable; printf ("% s \ n", p); str [0] = 'H'; // all elements of str are char-type variables. to modify the character, it needs to be defined as an array; printf ("% s \ n", str); return 0 ;}
<Span style = "color: # ff0000;"> // use a pointer to pass character arrays without passing the length of arrays, the string itself ends with '\ 0' </span> // change lowercase letters to uppercase # include <string. h> void convert (char * s) // change lowercase letters to uppercase letters {printf ("in convert s is % s \ n", s); int len = (int) strlen (s); for (int I = 0; I <len; I ++) {if (* (s + I)> = 'A' & * (s + I) <= 'Z') // if it is a letter, convert it to {* (s + I) -= 'a'-'A' ;}} char * convert (char * s) // The value of the general character array needs to be returned {char * p = s; while (* p) // enter the loop when * p is not equal to 0 {if (* p> = 'A' & * p <= 'Z ') {* p-= 'a'-'A';} p ++;} return s;} int main () {char str [100] = "helleworld "; convert (str); printf ("in convert s is % s \ n", str); return 0 ;}
10:
/*
"Hello" --> "heLLo" is replaced by the given l -->
Replace (char * s, char old, char new)
*/
Char * replace (char * s, char old, char new) {char * p = s; while (* p) {if (* p = old) {* p = new;} p ++;} return s ;}// calculate the length of the string to implement the strlen function; int myStrlen (char * s) {char * p = s; int count = 0; while (* p) {count ++; p ++;} return count ;} int main () {char str [100] = "helloworld"; replace (str, 'l', 'y'); printf ("% s \ n", str ); int len = myStrlen (str); printf ("% d \ n", len); return 0 ;}
// Insert a character into the string
// Pos indicates the insert position, and ch indicates the inserted characters;
Char * insert (char * s, int pos, char ch) {int len = (int) strlen (s); char * p = s; int count = 1; while (* p) // enter the loop when * p is not equal to 0 {if (pos = len + 1) // If the element to be inserted is at the last position of the character array, that is, the original '\ 0' is directly assigned as the target character when' \ 0' is located, then exit the loop {* (s + len) = ch; break;} if (pos = count) // If the inserted position is any position in the middle of the array when entering the judgment {for (int I = len-1; I> = pos-1; I --) // Insert the array into the position (including the insertion position) and move all elements before '\ 0' back {* (s + I + 1) = * (s + I ); // that is, the first element before '\ 0' is moved to the' \ 0' position, and so on} * (s + pos-1) = ch; // assign the last element in the original array to the character to be inserted; break;} count ++; p ++;} return s ;} int main () {char str [100] = "helloworld"; insert (str, 1, 'M'); printf ("% s \ n", str); return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.