当前位置:网站首页>Review of arrays and pointers triggered by a topic
Review of arrays and pointers triggered by a topic
2022-06-25 14:51:00 【-Ximen blowing snow】
Catalog
subject
int a[2][2][3] = {
{
{1, 2, 3}, {4, 5, 6}}, {
{7, 8, 9}, {10, 11, 12}}};
int *ptr = reinterpret_cast<int*>(&a + 1);
qDebug() << *(reinterpret_cast<int*>(a + 1)) << *(ptr - 1);///> Output 7 12The output is 7 12
Their thinking
&a+i = a+i*sizeof(a);
a+i = a+i*sizeof(a[0]);Reference resources

Array review practice
Dip a few pictures




Under test
//--------------------------- Array
// Array initialization
int crd[4] = {4, 4, 4, 4}; ///>ok
int crd1[3];///>ok
// 1- Non assignable error :error: array type 'int [3]' is not assignable, Non assignable
// crd1 = {3,3,3};
// crd1 = crd;
// Partial initialization
double tips[5] = {3.44, 5.2};///> Partial initialization , Other settings are 0
qDebug() << tips[0] << tips[2]; ///> Output 3.44 0
//[] It's empty time , The compiler calculates the size of the array , But this is a very bad practice , But this is better when the character array is initialized to a string
short tips2[] = {2, 3, 4, 5, 5, 5, 5, 5};
qDebug() << sizeof (tips2);///> Output 16 = 2*8
//c++11 Initializing the array
//1- Omit the equal sign
double ee[4] {1.2, 2.3, 3, 3};
//2-{} It doesn't contain anything
double ee1[4] = {};///> Initialize to 0
double ee2[4] {};
//3- List initialization base narrowing conversion , Insert an explicit cast to eliminate this problem
// long ee3[4]{1.2,2.3,3,3};///>error: type 'double' cannot be narrowed to 'long' in initializer list
char ee4[3] {11, 22, '1'}; ///>ok
// Character array initialization
char str[3] = {'2', '2', '3'}; ///> Not a string
char str1[3] = {'2', '2', '\0'}; ///> Is a string
// Literal constant initialization
char str2[] = "123456";
// char str3[7] = "1234567";///> error: initializer-string for char array is too long
char str4[7] = "123456";
qDebug() << sizeof (str2) << sizeof (str4);///> Output 7 7
// "123456" What is actually identified is the memory address where the string is located
// char addr = "123";///>error: cannot initialize a variable of type 'char' with an lvalue of type 'const char [4]'Pointer review practice
Dip chart



Under test
int i1 = 6;
long long i2 = 3;
quint64 i3 = 333;
int *p = &i1;
long long *p1 = (static_cast<long long *>(&i2));
quint64 *p2 = &i3;
qDebug() << sizeof (p) << sizeof (p1) << sizeof (p2);///> Output :4 4 4
int *p3, p4; ///>p3 It's a pointer ,p4 It's a int
//1 A function pointer
double pf1(int);///> function
double (*pf)(int);///> A function pointer ,pf Is a pointer to a function
// double*pf1(int)///> A return value is double The script of the pointer , It is not allowed to define a function here
pf = pf1;///>pf Is a pointer pf1() The pointer ; notes :pf1() The return value and parameters of the function must be the same as pf Same type of
double pf2(double);
/**
error: assigning to 'double (*)(int)' from incompatible type 'double (double)':
type mismatch at 1st parameter ('int' vs 'double')
*/
//pf = pf2;
//2 Call a function... Using a function pointer , Either way
double vv = pf(3);///> Use the function name
double vv1 = (*pf)(4);///> Use function pointers , This way is clear
//3 More complex function pointers
//3-1 The characteristic values and return values of the following functions are the same
const double * f1(const double ar[],int n);
const double * f2(const double [],int n);
const double * f3(const double *,int n);
//3-2 Declare a function pointer
//3-2-1
const double * (*f4)(const double ar[],int n);///> First of all
f4 = f1; f4 = f2; f4 = f3;
//3-2-2
const double * (*f5)(const double ar[],int n) = f1;///> Initialize when declared
//3-2-3 C++ Infer for yourself
auto f6 = f1;
//3-2-4 Declare an array of three pointers ,[] Has a higher priority than * ,*f7[3] Is an array of three function pointers ,(*f7)[3], So it's a pointer , An array that points to three elements
const double * (*f7[3])(const double ar[],int n) = {f1,f2,f3};///> Initialize when declared
//3-2-4-2 The above situation cannot be used auto,auto Can only be used for single valued initialization
auto f8 = f7;
//3-2-4-3 Call function
double aa[] = {1.1,2.2,3.3};
const double * ppx = f7[0](aa,3);
const double * ppx1 = (*f8[1])(aa,3);
//3-2-4-4 Get directions double Value
double ppxv = *f7[0](aa,3);
double ppx1v = *(*f8[1])(aa,3);
/**
Be careful :*f7[3] in f7 yes &f7[0],&f7 Is the address of the entire array ( An address containing three pointers ),f7+1 Is the next element in the array ,&f7+1 It is sizeof(f7)+f7
*/
边栏推荐
- [untitled]
- To make pytorch faster, you need to master these 17 methods
- Clinical chemistry | zhangjianzhong / Xu Jian develop single cell precision diagnosis and treatment technology for Helicobacter pylori
- Thymeleaf Usage Summary
- Supplementary inheritance and strict mode
- How to make GIF animation online? Try this GIF online production tool
- C language escape character and its meaning
- Qt: Pro project file
- Use Matplotlib to draw a line chart
- 网上股票开户安不安全?有谁知道呢
猜你喜欢

多张动图怎样合成一张gif?仅需三步快速生成gif动画图片

电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备

The best screenshot tool in the world, color absorption tool snipaste

Flexible layout (display:flex;) Attribute details

【中国海洋大学】考研初试复试资料分享

In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried

Does stream even have application advanced learning? As a programmer, you know what

Clinical chemistry | zhangjianzhong / Xu Jian develop single cell precision diagnosis and treatment technology for Helicobacter pylori

Uniapp cloud packaging app

JS to add elements to the header, or tail of an array
随机推荐
Clinical Chemistry | 张建中/徐健开发幽门螺杆菌单细胞精准诊疗技术
C language escape character and its meaning
Haven't you understood the microservice data architecture transaction management +acid+ consistency +cap+base theory? After reading it, you can completely solve your doubts
Clipboard tutorial
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
Kubernetes 理解kubectl/调试
QT database connection
JS functions
【深度学习】多标签学习
SPARQL learning notes of query, an rrdf query language
Usage of pure virtual functions
Sequential programming 1
Master XSS completely from 0 to 1
The best time to buy and sell stocks
如何裁剪动图大小?试试这个在线照片裁剪工具
14 -- validate palindrome string II
Study notes of cmake
Two common ways for orcale to clear table data
Remove interval (greedy)
Go语言Zap库Logger的定制化和封装使用详解