当前位置:网站首页>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
边栏推荐
- [ThreadX] h743 + ThreadX + Filex migration record
- copy constructor
- [ThreadX] h743zi + lan8720 + ThreadX + netx duo transplantation
- 对象数组与对象指针
- gcc ,g++,gdb的安装
- Cross domain issues - allow origin header contains multiple values but only one is allowed
- C#中?的这种形式
- For() loop parameter call order
- Object array and object pointer
- Robocode教程7——雷达锁定
猜你喜欢
浮点数双精度,单精度以及半精度知识总结
[untitled]
【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (2)
类和对象的初始化(构造函数与析构函数)
拷贝构造函数
C语言的运算符
Dynamic creation and release, assignment and replication of objects
Robocode教程5——Enemy类
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit
File viewing commands and user management commands
随机推荐
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit
友元函数,友元类,类模板
客户端软件增量更新
进程间通信-互斥锁
Detailed arrangement of knowledge points of University probability theory and mathematical statistics
爬西瓜视频url
Opencv uses genericindex for KNN search
Static member
基于VGG卷积神经网络的图像识别代码实现
对象的动态建立和释放,赋值和复制
vs中能编译通过,但是会有红色下划线提示未定义标示符问题
猜数字游戏
修改注册表的值
Qt 添加QSerialPort类 实现串口操作
Generate random number
爬取彩票数据
基于TensorFlow的线性回归实例
类和对象
猜數字遊戲
Dynamic creation and release, assignment and replication of objects