C language ---------- pointer, ---------- pointer
Address
Note: the pointer is equivalent to the door number and the envelope address. Now I can see the pointer and think of the door number and the envelope address.
#include "stdio.h"int main(){ int a = 10; int *p = &a; printf("%p\n", p); return 0; }// output
0x7fff5fbff81c
Analysis: it is a specific location in the system RAM, usually expressed in hexadecimal numbers. The system can find the corresponding content through this address. When 80386 is used, we must distinguish the following three addresses: Logical Address, linear address, and physical address. in C Language pointer programming, we can read the value of the pointer variable itself (& operation ), in fact, this value is the logical address, which is relative to the address (offset address) of the data segment of your current process and is not related to the absolute physical address, such as"0x7fff8b6a0000c"Is the logical address. The logical address is not directly sent to the memory bus, but to the memory management unit (MMU ). MMU is composed of one or more chips. Its function is to map logical addresses to physical addresses, that is, address translation. The conversion relationship diagram is as follows:
Pointer
I. Definition of pointer Variables
Pointer: the pointer of a variable is the address of the variable (the address is the pointer)
Pointer variable: the variable that stores the variable address. It is used to point to another variable.
1. Format: variable type * pointer variable name;
2. Example: int * p; char * p2;
3. Note: * when defining a variable, it is only a symbol of the pointer variable.
2. Using pointer variables to modify values of other variables
1. point to a variable
Int;
Int * p;
P = &;
Or
Int * p = &;
2. Modify the value of the variable to be pointed
* P = 10;
3. Modify the value of the variable outside the function.
int a = 10;change(&a);void change(int *n){ *n = 20;}
4. pointer usage notes:
int main(){
/* Not recommended syntax. int * p can only point to int type data int * p; double d = 10.0; p = & d; * // * the pointer variable can only store the address int * p; p = 200; * // * the pointer variable is not initialized and cannot be used to indirectly access other buckets int * p; printf ("% d \ n", * p); */int a = 10;/* int a; a = 10; * // * int * p; p = & a; * // when defining a variable, * is just a symbol and has no other special meaning. int * p = &; // incorrect statement // * p = & a; // The role of * at this time: access the bucket pointed to by the variable p * p = 20; char c = 'a'; char * cp = & c; * cp = 'D '; printf ("% c \ n", c );
Return 0;
}
// output
D
Exercise summary:
# Include <stdio. h> void swap (int * v1, int * v2); int main () {/* int a = 10; int B = 11; swap (& a, & B ); */int a2 = 90; int b2 = 89; swap (& a2, & b2); printf ("a2 = % d, b2 = % d \ n", a2, b2); return 0;}/* the value of an external real parameter cannot be exchanged. It only exchanges the internal pointer to void swap (int * v1, int * v2) {int * temp; temp = v1; v1 = v2; v2 = temp;} * // complete the void swap (int * v1, int * v2) Interchange Between Two integer variable values) {int temp = * v1; * v1 = * v2; * v2 = temp;}/* only the void swap (int v1, int v2) values of internal v1 and v2 are exchanged) {int temp = v1; v1 = v2; v2 = temp ;}*/
Pointer questions :/*
% D int % f float \ double % ld long % lld long % c char % s string % zd unsigned long */# include <stdio. h>/* 0000 0001 0000 0010 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010 0000 0001 */int main () {// 0000 0000 0000 0000 0000 0000 0000 0010 int I = 2; // 0000 0001 char c = 1; char * p; p = & c; // * p = 10; printf ("the value of c is % d \ n", * p); return 0;} void test () {char c; // 1 int a; // 4 long B; // 8 // any pointer occupies 8 bytes of storage space char * cp; int * ap; long * bp; printf ("cp = % zd, ap = % zd, bp = % zd \ n", sizeof (cp), sizeof (ap), sizeof (bp ));
}
3. pointers and Arrays
1. When an array is passed in as a function parameter, it is automatically converted to a pointer.
/* 1. array Element access method int ages [5]; int * p; p = ages; 1> array name [subscript] ages [I] 2> pointer variable name [subscript] p [I] 3> * (p + I) 2. pointer variable + 1. How much the address value is added depends on the pointer type int * 4 char * 1 double * 8 */void change (int array []); int main () {// 20 bytes int ages [5] = {10, 11, 19, 78, 67}; change (ages); return 0 ;} // use a pointer to receive an array. the pointer variable array points to the first element of the array void change (int * array) {printf ("% d \ n ", array [2]); // printf ("% d \ n", * (array + 2);}/* void change (int array []) {int s = sizeof (array); printf ("% d \ n", s);} */void test () {double d = 10.8; double * dp; dp = & d; printf ("dp = % p \ n", dp); printf ("dp + 1 = % p \ n", dp + 1 ); int ages [5] = {10, 9, 8, 67, 56}; int * p; // The pointer Variable p points to the first element of the array p = & ages [0]; // The array name is the address of the array and the address of the first element of the array // p = ages; /* p ---> & ages [0] p + 1 ---> & ages [1] p + 2 ---> & ages [2] p + I ---> & ages [I] * // printf ("% d \ n ", * (p + 2); printf ("% d \ n", p [2]);/* for (int I = 0; I <5; I ++) {printf ("ages [% d] = % d \ n", I, * (p + I);} * // printf ("% p \ n ", p); // printf ("% p \ n", p + 1); // printf ("% p \ n", p + 2 );}
4. pointer and string
1. Constant Zone
Store some constant strings
2. Heap
Object
3. Stack
Store Local Variables
MASTER:
Two Methods for defining strings
1> Use Arrays
Char name [] = "itcast ";
* Features: the characters in the string can be modified.
* Usage: the string content needs to be modified frequently.
2> Use Pointer
Char * name = "itcast ";
* Feature: the character string is actually a constant string, and the characters in it cannot be modified.
* Usage: the content of a string does not need to be modified, and this string is often used.
Int main () {char name [20]; printf ("Enter name: \ n"); scanf ("% s", name ); // 'J' 'a 'C' k'' \ 0' // printf ("% c \ n", name [3]); // printf ("the input string is % s \ n", name); return 0 ;}// define the string array void test2 () {char * name = "jack"; // int ages [5]; // pointer array (String Array) char * names [5] = {"jack ", "rose", "jake"}; // two-dimensional character array (String Array) char names2 [2] [10] = {"jack", "rose "};} // define the string void test () {// string variable char name [] = "it"; name [0] = 'T '; // printf ("% s \ n", name ); // "it" = 'I' + 'T' + '\ 0' // the pointer variable name2 points to the first character of the string // String constant char * name2 = "it "; char * name3 = "it"; // * name2 = 'T'; // printf ("% c \ n", * name2 ); printf ("% p \ n", name2, name3); // printf ("% s \ n", name2 );}