当前位置:网站首页>C | array and pointer
C | array and pointer
2022-07-21 15:55:00 【이 hola】
List of articles
Array
The types of arrays are Element type and number decision
Array size Variables cannot be used to define , It can only be one Positive integer constant
When an array is passed as a formal parameter of a function, it will degenerate into a pointer
The array name represents the first address of the array a == &a[0]
Two dimensional array
- Type name Array name [ That's ok ] [ Column ];
- Two dimensional arrays are essentially composed of Multiple one-dimensional arrays form .
int ar[3] [4]: It consists of three one-dimensional arrays , The size of each one-dimensional array is 4 An integer element
When initializing a two-dimensional array , One dimensional length can be defaulted , The length of two dimensions cannot be defaulted
Pointer to array
int *p[10]: Pointer array The first is an array Array has 10 Elements Every element is a pointer
int (*p)[10] : Row pointer Pointer to the variable contain 10 One dimensional array of elements
Example 1: The elements of rows and columns of a two-dimensional array are exchanged
int main()
{
int ar[2][3] = {
1,2,3,4,5,6 };
int br[3][2] = {
0 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
br[j][i] = ar[i][j];
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << br[i][j] << " ";
}
cout << endl;
}
return 0;
}
Yang hui triangle
Example 2: Yang hui triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
int ar[10][10]; // Use two-dimensional array to realize
for (int i = 0; i <= 10; i++)
{
for (int j = 0; j <= i; j++)
{
if (i == j || j == 0)
{
ar[i][j] = 1;
}
else
{
ar[i][j] = ar[i - 1][j] + ar[i - 1][j - 1];
}
}
}
int ar[10]; // Use one-dimensional array to realize
for (int i = 0; i <= 10; i++)
{
ar[i] = 1;
for (int j = i - 1; j > 0; j--)
{
ar[j] = ar[j] + ar[j - 1];// The number in the middle of a line is equal to the sum of the corresponding position numbers in the previous line
}
for (int j = 0; j <= i; j++)
{
cout << ar[j] << "\t";
}
cout << endl;
}
Flexible array
Flexible array The array size is declared as 0, Or do not give a size called a flexible array
The size of a page is 4k
struct sd node
{
int num;
int size;
char data[]; // The last element of only one structure is an array
}; // The characteristics of array development : Open it wide and use it small
sizeof(struct sd node) = 8; // Flexible arrays do not take up storage space
struct kd_node //8
{
int num;
int size;
char data[];//data[0]
};
int main()
{
struct kd_node* p1 = (struct kd_node*)malloc(sizeof(struct kd_node)+20);
if(nullptr == p1)
{
exit(EXIT_FAILURE);
}
strcpy(p1->data,20,"yhping");
p1->num = strlen("yhping");
p1->size = 20;
free(p1);
p1 = nullptr;
return 0;
}
Give... At one time 28 byte
int main()
{
struct kd_node s1 = {
6,7,"yhping"};
struct kd_node s2 = {
11,12,"yhpinghello"};
}
//sizeof(s1) => 8 Determine the size at compile time
//sizeof(s2) => 8
struct kd_buff //12
{
int num;
int size;
char* buff;
}
kd_buff* p = (kd_buff*)malloc(sizeof(kd_buff));
p->num = 6;
p->size = 7;
p->buff = (char*)malloc(sizeof(char) * 7);
strcpy_s(p->buff, 7, "yhping");
free(p);free(p->buff);
The pointer
Definition
- All data in the computer must be stored in memory , Different types of data occupy different bytes . In order to access data correctly , Each byte must be numbered , take The number of bytes in memory is called address or pointer . Address from 0 Start by increasing .
- In the pointer variable definition , * Combined with variable name
- The size of the pointer is 32 Is it 4 individual byte ,64 Is it 8 Bytes
- The value of the pointer variable itself stores the address , The entity to which the pointer variable refers ( Quoting )
- When defining pointer variables , Types have two functions for pointer variables :
1) Resolve the size of the storage unit
2) Pointer variable plus 1 The ability of int Add 1 Add four bytes - Before using pointer variables, be sure to initialization , Prevent the wild pointer
- The pointer Different types , You can't assign values to each other , A cast must be done
- The meaning of
int c = a* b; Multiplication sign
int * p = &a; Pointer to the variable ,
*p = 100; Quoting , Get the data that the pointer points to , Indirect operation
int a = 10, b = 20;
int* pa = &a;
int* pb = &b;
if (*pa > *pb) {
cout << 1 << endl; } // Compare the data pointed by the pointer 10 20
else cout << 2 << endl;
if (pa > pb) {
cout << 3 << endl; } // Compare the address of the pointer
else cout << 4 << endl;
void Swap(int a, int b) // Value passed
{
int c = a;
a = b;
b = c;}
void Swap_r(int* a, int* b) // Pointer passing Address
{
int c = *a;
*a = *b;
*b = c;}
int main()
{
int x = 10, y = 20;
Swap(x, y);
cout << x << " " << y << endl; //10 20
Swap_r(&x, &y);
cout << x << " " << y << endl; //20 10
void fun(int* p)
{
int a = 200;
*p = 100;
p = &a;
}
int main()
{
int x = 0;
int* s = &x;
fun(s);
cout << x << "\t" << *s << endl; //100 100
The operation of the pointer
notes : Pointer operations can easily go beyond the bounds of the array , Pay attention to the problem of cross-border
- Type plus 1 Add sizeof(int) Bytes
const int n = 5;
int ar[] = {
12,23,34,45,56 };
int* ip = &ar[0];
for (int i = 0; i < n; i++)
{
cout << ip << "->" << *ip<< endl;
ip += 1; //int type ip+1 Add four bytes
}
The result of pointer and integer addition and subtraction is still pointer type
Pointers and integers i The addition and subtraction of is equal to the pointer value ( Address ) And i*sizeof( The target type ) Addition and subtraction of product , Get a new addressThe pointer - The pointer
1) Two pointers of the same type , Just think that continuous space can be subtracted , The result of subtraction is the size of data elements
int Type a pointer -int Type a pointer Is the number of integer elements
2) If and only if two pointer variables of the same type point to elements in the same array , You can use relational operators >,==,!= Compare . The comparison rule is that the pointer to the following element is high , An equality that points to the same element
const And a pointer
const With pointers, you can limit pointer variables ; Limit the data pointed to by pointer variables
- Limit the pointer variable itself int * const p;
The value of the pointer variable itself cannot be modified , The direction can be changed , therefore const Decorated pointer variables can only be initialized at definition time , Cannot define after assignment
int * const ip = &a; // Initialization is required when defining
*ip = 200; //ok
ip = &b; //err
- Limit the data pointed to by pointer variables const int * ip; int const * ip;
Pointers can point to different variables , However, you cannot use a pointer to modify the value of the pointer pointing to data
int const * ip = NULL;
ip = &b; //ok
ip = &a; //ok
*ip = 100; //err
- Limit pointer variables and the value of pointer variables pointing to data const int * const ip;
const int * const ip = &a;
// The first modifier points to
// The second modifier pointer itself
notes : const The modified content cannot be used as an lvalue
The address of a constant cannot be disclosed to a non constant pointer
const The type of modifier is the nearest first formed pointer 、 The rest is what it modifies
If const Embellished The content does not contain a pointer , Cannot participate in type
* and &
int a=10;int * ap = &a;
1)*&a: * (&a),&a It means to take a variable a The address of , *(&a) It means to get the data in the address , Equivalent to a
2)& ap:& ( * ap), * ap Express ap The data in the address ,& ( * ap) The address of the data , Equivalent to ap(a The address of )
3) * &ap: (&ap),&ap It means to take a variable ap The address of , *(&ap) It means to get the data in the address , Equivalent to &a(ap The data in the address is a The address of &a)
No type pointer void* Generic pointer
void You can't define variables , But you can define pointer variables ,void The pointer can point to the address of any type of variable
void Pointer variables are called generic pointers , Pointer can be given void Pointer variable assignment
If you will void Pointers are assigned to other types of pointers , You need to cast
int a =0; char ch = 'x';
void* vp = &ch;
vp = &a;
int *ip = (int *)vp;
The secondary pointer
If a pointer is pointing to another pointer , It is called secondary pointer ( The pointer to the pointer )
Pointers and arrays
The relationship between pointer and array
The array name is automatically converted into a pointer variable to the first element of the array in the expression
ar Represents the address of the first element of the array * ar Represents the first element of the array ar[0]
ar+1 Represents the address of the second element of the array , *(ar+1) Represents the second element of the array ar[1]
*ar[i] Interpreted by the compiled system as (ar+i)
The difference between a pointer and an array
- Modify the content
- Content replication and comparison
- Calculate the memory capacity
Common pointer variables
边栏推荐
- [test report in July] Oracle OCP 19C exam passed
- 小程序毕设作品之微信疫苗预约小程序毕业设计(1)开发概要
- Solve the sudden jump out of the command line (flashing)
- 每日一题-LeetCode1260-二维网格迁移-数组-映射
- 多御安全浏览器下载被阻止怎么办?关闭下载阻止的方法
- A solution to std:: string cannot cross DLLs
- Training of head and neck segmentation networks with shape prior on small datasets
- Shape and boundary-aware multi-branch model for semi-supervised medical image segmentation
- 十大券商开户有没有风险的?安全靠谱吗?
- Survey and Systematization of 3D Object Detection Models and Methods(3D目标检测模型与方法综述)论文笔记
猜你喜欢
【数字图像处理/双边滤波实验】高分课程实验报告分享
解决突然跳出命令行(闪烁)
2022/07/20 学习笔记 (day12)String字符串
ESP8266-NodeMCU——使用WiFiManager库连接wifi
Easygbs platform setup tips: how to hide the platform web page from being accessed?
目前最火的测试框架,pytest封神级讲解
脸书将缩减新闻产品 相关资源侧重元宇宙和短视频内容创造
-Solid modeling-
Algorithm summary] 20 questions to complete bat interview - binary tree
sha256为什么不可逆,sha256的安全性如何
随机推荐
Source code, complement and fixed-point number operation
ESP8266-NodeMCU——使用U8g2库点亮OLED
Training of head and neck segmentation networks with shape prior on small datasets
Token and refresh_ token
Training of head and neck segmentation networks with shape prior on small datasets
[shutter -- actual combat] quick start shutter
I2C adapter driver
Moment plug-in - time formatting
ESP8266-NodeMCU——使用WiFiManager库连接wifi
Wechat vaccine appointment applet graduation design of applet completion work (2) applet function
-Solid modeling-
小程序毕设作品之微信疫苗预约小程序毕业设计(4)开题报告
Is there any risk for the top ten securities companies to open accounts? Is it safe?
获取当前 标注样式dimstyle
如何让localStorage支持过期时间设置?
Projection & local illumination
Wechat applet realizes the playback effect of Netease cloud music recorder
TL494电源芯片使用记录分享
数据库约束&&MySQL进阶查询
【Flutter -- 实战】快速入门 Flutter