当前位置:网站首页>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 12
The 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
*/
边栏推荐
- 14 -- validate palindrome string II
- New title of PTA
- 开餐馆
- Biscuit distribution
- [untitled] the CMD command window displays' NPM 'which is not an internal or external command
- Luogu p5707 [deep foundation 2. example 12] late for school
- Tencent cloud builds a Socks5 multi IP proxy server to realize the perfect building of a game with a single window and a single IP. Tutorial attached tool "suggestions collection"
- Remove interval (greedy)
- QT file reading -qfile
- Explanation of dev/mapper
猜你喜欢
Uniapp cloud packaging app
开餐馆
电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备
C language escape character and its meaning
Kubernetes 理解kubectl/调试
Thymeleaf Usage Summary
Heavyweight! The domestic IDE is released and developed by Alibaba. It is completely open source! (high performance + high customization)
Summary of common functions in Oracle Database
合宙Air32F103CBT6开发板上手报告
QQ情话糖果情话内容获取并保存
随机推荐
Variables, scopes, and variable promotion
What moment makes you think there is a bug in the world?
Reading the "clean" series for the first time, I didn't think it was a good book
Gif动图如何裁剪?收下这个图片在线裁剪工具
What is the safest app for stock account opening? Tell me what you know
Report on Hezhou air32f103cbt6 development board
Compile Caffe's project using cmake
Common classes in QT
Common formatting methods for amount numbers
【Try to Hack】vulnhub DC1
New good friend Pinia, leading the new era of state management
【深度学习】多标签学习
JS to verify whether the string is a regular expression
Extend JS copy content to clipboard
JS Base64 Library Learning
QQ情话糖果情话内容获取并保存
Arithmetic operations and expressions
Usage of qlist
NBD Network Block Device
【HBZ分享】LockSupport的使用