当前位置:网站首页>指针与数组&指针与const&结构体与const
指针与数组&指针与const&结构体与const
2022-06-12 21:01:00 【又秃又弱】
指针与数组:
指针可访问数组下标
例如:
void Show(int* p, int length)//输出函数
{
for (int i = 0; i < length; i++)
printf("%d", p[i]);//或者printf("%d", *(p+i));p[i]等价于*(p+i)
}
int main()
{
int arr[] = { 1,2,3,4 };
Show(arr, sizeof(arr) / sizeof(arr[0]));
}
指针与const:
const用于修饰函数和变量
const修饰常变量:
const int a=10;//a为常变量,不可修改
const int *与 int* const:
const int *p=&a; //允许p指向a,但是不可修改a,p可以任意指向。a是常量,不允许修改。
int* const p=&a; //此时,const修饰p变量,p只能指向a,也可修改a(当前仅当a是变量),但是p不可以任意指向。
(可以这样理解记忆,只记忆哪个不能变:const int *p=&a,const后面是int *p,因为*p=a,所以a的值不变 ;int* const p=&a,const后面是p,修饰p,p格子内容是a的地址,p被const,因此格子不变,p的指向不变。
参照这篇文章理解:我用#CSDN#这个app发现了有技术含量的博客,小伙伴们求同去《int const*与int * const》, 一起来围观吧 https://blog.csdn.net/qq_33248019/article/details/89462405?utm_source=app&app_version=5.0.0
const的错误使用:
(1)const int a=10;
int* p=&a;//因为p不允许修改a,所以编译器提醒
(2)const int *p=&a;//限定P不修改a
*p=&b;
(3)const int a=10;
int *const p=&a;//会报错,这样写法意思是p可以修改a的值,但是a又是常变量
const char* name和char name[10]的区别:
1.内存上:指针占用4个字节,数组的内存可能比较大
2.想要修改字符,必须是数组。
结构体与const:
有个理解的知识点要强调:
eg:
struct Student
{
const char* name;
int age;
char sex;
};
int main()
{
struct Student s1 = { "李四",14,"男"};
}
说明:在结构体内定义const char* name,就致命不能修改名字,用法正确。
但若是const修饰性别,可以将s1.sex=‘女’修改指向。“男”修改为“女”,但不能将“男”修改为“男性”,意思就是 不是修改原来的字符换,而是修改了变量的指向。
边栏推荐
- Design and practice of Hudi bucket index in byte skipping
- Why my order by create_ Time ASC becomes order by ASC
- Research Report on hydraulic solenoid valve industry - market status analysis and development prospect forecast
- (11) Image frequency domain filtering with OpenCV
- To understand Devops, you must read these ten books!
- MySQL field truncation principle and source code analysis
- EU officially released the data act, Ukraine was attacked by DDoS again, kitchen appliance giant Meiya was attacked, internal data leakage network security weekly
- New product release Junda intelligent integrated environmental monitoring terminal
- A blog written clearly by vit
- Introduction to the characteristics of building a balancer decentralized exchange market capitalization robot
猜你喜欢

Data visualization - histogram

Vs2017 environmental issues

Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"

typeScript的定义类型:不能将类型“Timeout”分配给类型“number”;

Typescript definition type: type 'timeout' cannot be assigned to type 'number';

Data visualization - Calendar chart

Data visualization - broken line area chart

Data visualization diagram microblog forwarding diagram

(11) Image frequency domain filtering with OpenCV

The salted fish has been transmitted for 5W times, and the latest spring recruit face-to-face test questions of bytes have been leaked
随机推荐
Li Mu [practical machine learning] 1.4 data annotation
#141 Linked List Cycle
[tutorial] detailed explanation of the page rules parameter settings of cloudflare CDN
Data visualization - Calendar chart
Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?
Lua pattern matching
Double carbon in every direction: green demand and competition focus in the calculation from the east to the West
MySQL field truncation principle and source code analysis
Gather function in pytorch_
String Basics
居家办公期间如何提升沟通效率|社区征文
InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY EB3E94ADBE1229CF
Can tonghuashun open an account? Can tonghuashun directly open the security of securities companies on the app? How to open an account online when buying stocks
做自媒体视频,友好的新媒体运营必备app分享
Integrated monitoring solution for power environment of small and medium-sized computer rooms
机器学习资料汇总
A blog written clearly by vit
Understanding of closures
字符串基础知识
Foreign brands become a thing of the past? What are the key words of the TV industry in 2022?