当前位置:网站首页>Notes on advanced points of C language 2
Notes on advanced points of C language 2
2022-04-23 06:41:00 【~Ha】
Catalog
- Four 、 The pointer
7、 Strings and pointers
8、 Array pointer
9、 Array name and address
10、 The difference between pointer variables and array names
11、 Conversion of pointers in multidimensional arrays
12、 The pointer is used as the parameter of the function
- Four 、 The pointer
7、 Strings and pointers
a、 char string[100] = “I love C!”
Defines a character array string, Used to store multiple characters , And use ”I love C!” to string Array initialization
character string “I love C! ” Store in string in
b、 char *str = “I love C!”
A pointer variable is defined str, Only character address numbers can be stored ,
So I love C! The characters in this string cannot be stored in str In pointer variables .
str Just stored characters I The address number of , “I love C! ” Stored in the text constant area
c、 char *str =(char*)malloc(10*sizeof(char));// Dynamic application 10 Bytes of storage space , First address to str assignment .
strcpy(str, "I love C"); // The string “I love C!” copy to str Point to the memory
A character array : In the memory ( Stack 、 Static global area ) It opens up a space for storing strings
Modifiable , Can be initialized directly
String pointer : stay Text constant area Opened up a space to store strings , Pay the first address of the string to str
Do not modify the , Can be initialized directly
Pile up : Use malloc The function requests space in the heap , Copy the string to the heap
Modifiable , Cannot initialize directly
Whether the memory pointed to by the pointer can be modified depends on the position pointed to by the pointer , Pointing to the text constant area is not modifiable , Pointing stack 、 Pile up 、 The static global area can be modified
A character array : Use scanf perhaps strcpy
buf_aver="hello kitty"; // error , Because the name of the character array is a constant
strcpy(buf_aver,"hello kitty"); // correct
scanf("%s",buf_aver); // correct
Pointer to string :
buf_point="hello kitty"; // correct ,buf_point Point to another string
strcpy(buf_point,"hello kitty"); // error , read-only , Can you copy the string to buf_piont The memory pointed to depends on buf_point Where to point .
8、 Array The pointer ( Is a pointer )
Concept : Itself is a pointer , Point to an array , Add a jump to an array , That is, point to the next array
Define methods : The type of array pointed to (* Pointer variable name ) [ The number of elements of the array pointed to ]
int (*p)[5];// Defines an array pointer variable p, p Pointing to an integer, there are 5 Array of elements , That is, each small array has five integer variables
p+1 Point down 5 An integer , Skip one with 5 Array of integer elements .
difference Array pointer :int (*p) [5] Is a pointer , Only four bytes
int *p[5] It's an array , There are five shaping pointers
#include <stdio.h>
int main()
{
int a[3][5];// Defined a 3 That's ok 5 A two-dimensional array of columns
int(*p)[5];// Define an array pointer variable p, p+1 Jump one with 5 An integer array of elements
printf("a=%p\n",a);// The first 0 Row address of row
printf("a+1=%p\n",a+1);// The first 1 Row address of row , a and a +1 Bad 20 Bytes
p=a;
printf("p=%p\n",p);
printf("p+1=%p\n",p+1);//p+1 Jump one with 5 One dimensional array of integer elements
return 0;
}
$ One dimensional array pointer int(*p)[5] ; There are... For each line 5 individual int A two-dimensional array of type elements
$ Two dimensional array pointer int(*p)[4][5]; Use with a three-dimensional array , A three-dimensional array consists of several 4 That's ok 5 A two-dimensional array of columns
$ ...
Confusing content :
Pointer array : Is an array , A collection of pointers of the same type
int *p[10];
Array p Yes 10 individual int * Pointer variables of type constitute , Namely p[0] ~p[9]
Array pointer : Itself is a pointer , Point to an array , Add 1 Jump an array
int (*p)[10];
P It's a pointer , p Is an array pointer , p Add 1 Point to the next array , jump 10 A plastic .
The pointer of the pointer :
int **p;//p It's the pointer of the pointer , It can be saved int * Type of address
int *q;
p=&q;
9、 Array name and address
The pointer : Variable takes address
The pointer of the pointer : The pointer variable takes the address
Array pointer : Array name and address
int a[10];
a+1 Jump an integer element , yes a[1] The address of
a and a+1 One element apart , 4 Bytes
&a It becomes a one-dimensional array pointer , yes int(*p)[10] Type of .
(&a) +1 and &a The difference is an array, that is 10 The elements are 40 Bytes .
c Language policy , Array name and address , Becomes an array pointer . Add 1 Jump an array
10、 The difference between pointer variables and array names
int a[10];
int *p;
p = a;
The same thing :a and p Point to the same address , Equivalent when referring to array elements
Difference :a Is a constant ,p It's a variable. ;p You can assign ,a Can't assign a value
Yes a The fetch address is an array pointer , Yes p Take a pointer whose address is a pointer
11、 Conversion of pointers in multidimensional arrays
In a two-dimensional array , Line address take * Not worth it , It means that the pointer is degraded , By line address ( Array pointer ) Become the first in this line 0
Addresses of elements . take * The front and back still point to the same place , But the types of pointers are different
#include <stdio.h>
int main(int argc, char *argv[])
{
int a[3][5];
printf("a=%p\n",a);
printf("a +1=%p\n",a+1);
printf("*a =%p\n",*a);// *a Became the second 0 Xing di 0 The address of the column element
printf("(*a)+1 =%p\n",(*a)+1 ); // The result is 0 Xing di 1 The address of the column element
return 0;
}
12、 The pointer is used as the parameter of the function
When calling a function, pass the address of the variable , Pass... In the called function *+ The address changes the value of the variable in the stagnation function
To change the value of the variable in the main function , The address of the variable must be passed in , And you have to pass *+ Address To assign , No matter what type this variable is
void fun(char **q)
{
*q="hello kitty";
}
int main()
{
char *p="hello world";
fun(&p);
printf("%s\n",p);// The result is : hello kitty
}
When passing an array to a function, you can't pass all the array in at one time , Can only pass the address of the array
When passing a one-dimensional array :void fun(int p[]) perhaps void fun(int *p) // The two are equivalent , It's all pointers
When transmitting a two-dimensional array :void fun(int p[][4]) perhaps void fun(int (*p)[4])
When passing pointer array :
void fun(char **q) // char *q[]
{
int i;
for(i=0;i<3;i++)
printf("%s\n",q[i]);
}
int main()
{
char *p[3]={"hello","world","kitty"}; //p[0] p[1] p[2] char *
fun(p);
return 0;
}
版权声明
本文为[~Ha]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230548245455.html
边栏推荐
- [UDS unified diagnosis service] i. diagnosis overview (3) - ISO 15765 architecture
- The waterfall waterfall flow of uview realizes single column and loads more
- 【UDS统一诊断服务】一、诊断概述(3)— ISO 15765体系结构
- 浮点数双精度,单精度以及半精度知识总结
- 代理服务器
- 【UDS统一诊断服务】一、诊断概述(1)— 诊断概述
- 【OpenCV】使用 FileStorage 读写 Eigen 向量
- 深蓝学院激光slam 理论与实践 第三章激光雷达去畸变 作业习题
- C#【文件操作篇】PDF文件和图片互相转换
- C语言实现2048小游戏方向合并逻辑
猜你喜欢
【无标题】
【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (1)
C语言实用小技巧合集(持续更新)
For() loop parameter call order
PM2 deploy nuxt project
【UDS统一诊断服务】四、诊断典型服务(6)— 输入输出控制单元(0x2F)
[UDS unified diagnosis service] i. diagnosis overview (1) - diagnosis overview
[UDS unified diagnostic service] IV. typical diagnostic service (2) - data transmission function unit
Robocode教程8——AdvancedRobot
copy constructor
随机推荐
【无标题】
TP download folder, compress folder and download
Uniapp encapsulates request
_findnext 报错
CUDA环境安装
C语言进阶要点笔记3
Flask - 中间件
【UDS统一诊断服务】五、诊断应用示例:Flash Bootloader
OpenCV使用 GenericIndex 进行 KNN 搜索
四元数乘法
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit
识别验证码
搭建openstack平台
生成验证码
【UDS统一诊断服务】四、诊断典型服务(4)— 在线编程功能单元(0x34-0x38)
Feign请求日志统一打印
[ThreadX] h743zi + lan8720 + ThreadX + netx duo transplantation
软件工程中的十三种文档
for()循环参数调用顺序
Eigen 库常用基本用法 备忘