In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the knowledge points of CAccord + pointer". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Basic part
About memory
Memory meaning:
Memory: an important part of a computer that stores programs and data and assists CPU in computing.
Memory: internal memory, temporary program / data-power loss SRAM, DRAM, DDR, DDR2, DDR3.
External memory: external memory, keep programs / data for a long time-ROM, ERRROM, FLASH (NAND, NOR), hard disk, CD-ROM without power loss.
Memory is the bridge between CPU and hard disk:
Temporarily store operational data in CPU
Temporarily store data exchanged with external memory such as hard disk
Physical memory and storage address space
Two concepts about memory: physical memory and storage address space.
Physical memory: a physical memory chip that actually exists.
Memory sticks installed on the motherboard
Display RAM chip on display card
RAM chip and ROM chip on various adapter cards
Storage address space: the range of memory coding. The memory that we often talk about in software refers to this meaning.
Encoding: assign a number to each physical storage unit (one byte)
Addressing: you can find the corresponding storage unit according to the assigned number to read and write the data.
About memory address
Memory address
Abstract memory into a large array of one-dimensional characters.
Encoding is assigning a 32-bit or 64-bit number to each byte of memory (related to 32-bit or 64-bit processors).
This memory number is called the memory address.
Each piece of data in memory is assigned an address:
Char: assign an address to one byte
Int: four addresses assigned to four bytes
Float, struct, functions, arrays, etc.
Pointers and pointer variables
Pointer:
Each byte of the memory area has a number, which is the address.
If a variable is defined in a program, the system assigns a memory unit to the variable and determines its memory address (number) when the program is compiled or run.
The essence of the pointer is the memory "address". The pointer is the address, and the address is the pointer.
The pointer is the number of the memory unit, and the pointer variable is the variable where the address is stored.
Pointer variables are usually referred to as pointers when we describe them, but they actually have different meanings.
Definition and use of pointer variables
Pointer is also a data type, and pointer variable is also a variable.
Assign the address of the pointer variable to the pointer variable
The "*" operator operates on the memory space pointed to by the pointer variable.
# include int main () {int a = 0; char b = 100; printf ("% p,% p\ n", & a, & b) / print the address of a, b / / int* represents a data type, int* pointer type, p is the variable name / / defines a variable of pointer type, and can point to the address of an int type variable int* p; p = & a / / assigning the address of a to the variable pforce p is also a variable, and the value is a memory address number printf ("% d\ n", * p); / / p points to the address of a, * p is the value of a char * p1 = & b; printf ("% c\ n", * p1); / / * p1 points to the address of b, and * p1 is the value of b return 0 } indirectly modify the value of the variable int a = 0; int b = 11; int * p = & a; * p = 100; printf ("a =% d, * p =% d\ n", a, * p); p = & b; * p = 22; printf ("b =% d, * p =% d\ n", b, * p); pointer size
Use sizeof () to measure the size of the pointer, and the result is always: 4 or 8
Sizeof () measures the size of the pointer variable pointing to the storage address.
On a 32-bit platform, all pointers (addresses) are 32 bits (4 bytes).
On 64-bit platforms, all pointers (addresses) are 64 bits (8 bytes)
Wild pointer and null pointer
Pointer variables are also variables, and they can be assigned at will, without crossing the boundary (4 bytes for 32 bits and 8 bytes for 64 bits), but there is no point in assigning any numeric values to pointer variables because such pointers become wild pointers. The area the pointer points to is unknown (the operating system is not allowed to operate on the memory area that the pointer points to). Therefore, the wild pointer will not directly cause an error, and there will be a problem in the area of memory pointed to by the wild pointer.
Int a = 100; int * p; p = a; / / assigning the value of a to the pointer variable p is wild pointer, ok, it will not be a problem, but it does not make sense p = 0x12345678; / to assign a value to pointer variable p, p is wild pointer, ok, there will be no problem, but there is no meaning * p = 1000; / / wild pointer points to unknown area, memory problem, err
However, both wild pointer and valid pointer variables hold numeric values. In order to mark that this pointer variable does not point to any variable (free), you can assign NULL to this pointer in C language, thus indicating that the pointer is null and there is no pointer.
Int * p = NULL
NULL is a macro constant with a value of 0:
# define NULL ((void *) 0) Universal pointer void
The void * pointer can point to the memory space of any variable:
Void * p = NULL; int a = 10; p = (void *) & a; / / when pointing to variables, it is best to convert to void * / / when using the memory pointed to by pointer variables, convert to int * * ((int *) p) = 11; printf ("a =% d\ n", a); const modified pointer variables int a = 100; int b = 200 / / pointer to constant / / modifier *, pointer to memory area cannot be modified, pointer can be changed to const int * p1 = & a; / / equivalent to int const * p1 = & a; / * p1 = 111; / / err p1 = & b / / ok// pointer constant / / modifier p1, pointer pointing cannot be changed, memory pointed to by pointer can be modified int * const p2 = & a; / / p2 = & b; / / err * p2 = 222; / / ok pointer and array
Array name
The array name is the address of the first element of the array, but it is a constant:
Int a [] = {1,2,3,4,5,6,7,8,9}; printf ("a =% p\ n", a); printf ("& a [0] =% p\ n", & a [0]); / / a = 10 / / err, the array name is a constant and cannot modify the pointer operation array element # include int main () {int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int I = 0; int n = sizeof (a) / sizeof (a [0]); for (I = 0; I
< n; i++) { //printf("%d, ", a[i]); printf("%d, ", *(a+i)); } printf("\n"); int *p = a; //定义一个指针变量保存a的地址 for (i = 0; i < n; i++) { p[i] = 2 * i; } for (i = 0; i < n; i++) { printf("%d, ", *(p + i)); } printf("\n"); return 0;}指针加减运算 1)加法运算 指针计算不是简单的整数相加 如果是一个int *,+1的结果是增加一个int的大小 如果是一个char *,+1的结果是增加一个char大小 #include int main(){ int a; int *p = &a; printf("%d\n", p); p += 2;//移动了2个int printf("%d\n", p); char b = 0; char *p1 = &b; printf("%d\n", p1); p1 += 2;//移动了2个char printf("%d\n", p1); return 0;}通过改变指针指向操作数组元素:#include int main(){ int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int i = 0; int n = sizeof(a) / sizeof(a[0]); int *p = a; for (i = 0; i < n; i++) { printf("%d, ", *p); p++; } printf("\n"); return 0;}减法运算 示例1: #include int main(){ int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int i = 0; int n = sizeof(a) / sizeof(a[0]); int *p = a+n-1; for (i = 0; i < n; i++) { printf("%d, ", *p); p--; } printf("\n"); return 0;} 示例2: #include int main(){ int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int *p2 = &a[2]; //第2个元素地址 int *p1 = &a[1]; //第1个元素地址 printf("p1 = %p, p2 = %p\n", p1, p2); int n1 = p2 - p1; //n1 = 1 int n2 = (int)p2 - (int)p1; //n2 = 4 printf("n1 = %d, n2 = %d\n", n1, n2); return 0;}指针数组 指针数组,它是数组,数组的每个元素都是指针类型。 #include int main(){ //指针数组 int *p[3]; int a = 1; int b = 2; int c = 3; int i = 0; p[0] = &a; p[1] = &b; p[2] = &c; for (i = 0; i < sizeof(p) / sizeof(p[0]); i++ ) { printf("%d, ", *(p[i])); } printf("\n"); return 0;}多级指针 C语言允许有多级指针存在,在实际的程序中一级指针最常用,其次是二级指针。 二级指针就是指向一个一级指针变量地址的指针,三级指针基本用不着。 int a = 10; int *p = &a; //一级指针 *p = 100; //*p就是a int **q = &p; //*q就是p //**q就是a int ***t = &q; //*t就是q //**t就是p //***t就是a指针和函数 函数形参改变实参的值 #include void swap1(int x, int y){ int tmp; tmp = x; x = y; y = tmp; printf("x = %d, y = %d\n", x, y);}void swap2(int *x, int *y){ int tmp; tmp = *x; *x = *y; *y = tmp;}int main(){ int a = 3; int b = 5; swap1(a, b); //值传递 printf("a = %d, b = %d\n", a, b); a = 3; b = 5; swap2(&a, &b); //地址传递 printf("a2 = %d, b2 = %d\n", a, b); return 0;}数组名做函数参数 数组名做函数参数,函数的形参会退化为指针: #include //void printArrary(int a[10], int n)//void printArrary(int a[], int n)void printArrary(int *a, int n){ int i = 0; for (i = 0; i < n; i++) { printf("%d, ", a[i]); } printf("\n");}int main(){ int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int n = sizeof(a) / sizeof(a[0]); //数组名做函数参数 printArrary(a, n); return 0;}指针做为函数的返回值#include int a = 10;int *getA(){ return &a;}int main(){ *( getA() ) = 111; printf("a = %d\n", a); return 0;}指针和字符串 字符指针 #include int main(){ char str[] = "hello world"; char *p = str; *p = 'm'; p++; *p = 'i'; printf("%s\n", str); p = "mike jiang"; printf("%s\n", p); char *q = "test"; printf("%s\n", q); return 0;}字符指针做函数参数#include void mystrcat(char *dest, const char *src){ int len1 = 0; int len2 = 0; while (dest[len1]) { len1++; } while (src[len2]) { len2++; } int i; for (i = 0; i < len2; i++) { dest[len1 + i] = src[i]; }}int main(){ char dst[100] = "hello mike"; char src[] = "123456"; mystrcat(dst, src); printf("dst = %s\n", dst); return 0;}const修饰的指针变量#include #include #include int main(void){ //const修饰一个变量为只读 const int a = 10; //a = 100; //err //指针变量, 指针指向的内存, 2个不同概念 char buf[] = "aklgjdlsgjlkds"; //从左往右看,跳过类型,看修饰哪个字符 //如果是*, 说明指针指向的内存不能改变 //如果是指针变量,说明指针的指向不能改变,指针的值不能修改 const char *p = buf; // 等价于上面 char const *p1 = buf; //p[1] = '2'; //err p = "agdlsjaglkdsajgl"; //ok char * const p2 = buf; p2[1] = '3'; //p2 = "salkjgldsjaglk"; //err //p3为只读,指向不能变,指向的内存也不能变 const char * const p3 = buf; return 0;}指针数组做为main函数的形参int main(int argc, char *argv[]); main函数是操作系统调用的,第一个参数标明argv数组的成员数量,argv数组的每个成员都是char *类型 argv是命令行参数的字符串数组 argc代表命令行参数的数量,程序名字本身算一个参数 #include //argc: 传参数的个数(包含可执行程序)//argv:指针数组,指向输入的参数int main(int argc, char *argv[]){ //指针数组,它是数组,每个元素都是指针 char *a[] = { "aaaaaaa", "bbbbbbbbbb", "ccccccc" }; int i = 0; printf("argc = %d\n", argc); for (i = 0; i < argc; i++) { printf("%s\n", argv[i]); } return 0;}常用字符串应用模型 1.strstr中的while和do-while模型 利用strstr标准库函数找出一个字符串中substr出现的个数。 a) while模型 #include #include #include int main(void){ char *p = "11abcd111122abcd333abcd3322abcd3333322qqq"; int n = 0; while ((p = strstr(p, "abcd")) != NULL) { //能进来,肯定有匹配的子串 //重新设置起点位置 p = p + strlen("abcd"); n++; if (*p == 0) //如果到结束符 { break; } } printf("n = %d\n", n); return 0;} b) do-while模型 #include #include #include int main(void){ char *p = "11abcd111122abcd333abcd3322abcd3333322qqq"; int n = 0; do { p = strstr(p, "abcd"); if (p != NULL) { n++; //累计个数 //重新设置查找的起点 p = p + strlen("abcd"); } else //如果没有匹配的字符串,跳出循环 { break; } } while (*p != 0); //如果没有到结尾 printf("n = %d\n", n); return 0;} 2.两头堵模型 求非空字符串元素的个数: #include #include #include #include int fun(char *p, int *n){ if (p == NULL || n == NULL) { return -1; } int begin = 0; int end = strlen(p) - 1; //从左边开始 //如果当前字符为空,而且没有结束 while (p[begin] == ' ' && p[begin] != 0) { begin++; //位置从右移动一位 } //从右往左移动 while (p[end] == ' ' && end >0) {end--; / / move to the left} if (end = = 0) {return-2;} / / number of non-empty elements * n = end- begin + 1; return 0;} int main (void) {char * p = "abcddsgadsgefg"; int ret = 0 Int n = 0; ret = fun (p, & n); if (ret! = 0) {return ret;} printf ("number of non-empty string elements:% d\ n", n); return 0;}
3. String inversion model (inverted)
# include # include # include int inverse (char * p) {if (p = = NULL) {return-1;} char * str = p; int begin = 0; int end = strlen (str)-1; char tmp; while (begin < end) {/ / Exchange element tmp = str [begin] Str [begin] = str [end]; str [end] = tmp; begin++; / / move right position end--; / / move left position} return 0;} int main (void) {/ / char * str = "abcdefg" / / the constant area of the file, the content is not allowed to be modified char str [] = "abcdef"; int ret = inverse (str); if (ret! = 0) {return ret;} printf ("str =% s\ n", str); return 0;} "what are the pointer knowledge points?". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.