当前位置:网站首页>Pointer - character pointer
Pointer - character pointer
2022-07-06 00:18:00 【It's Yi'an】
Part1:
Array function parameter Character pointer Basic usage Constant string
When we pass an array into a function , In fact, what is passed in is an address , Modify the content in the address , On the sizeof What you get is not the space occupied by the array , It's the space occupied by the pointer .
#include<stdio.h>
void mylen(int arr[]) {
int sz = sizeof(arr) / sizeof(arr[0]);
printf("%d", sz);
}
int main(void) {
int arr[10] = { 0 };
mylen(arr);
return 0;
}
// The result is 1/2
Character pointer : We usually define a character first a, Define a character pointer variable to point to a.
char test='a';
char* p=&test;
Or define a character array , Define a pointer variable to point to it .
char arr[]="abcdef";
char* p=arr;
If we define a pointer variable directly , Then assign a character or string to the pointer variable , In fact, it is the address of the first character . Such a string is called a constant string .
char* p="abcdef";
Why is it called a constant string ? According to our previous understanding , We have a pointer to the first character. Can we modify it ?
#include<stdio.h>
int main(void) {
char* p = "abcdef";
*p = 'W';
printf("%s", p);
}
The program running here will crash . But the compiler does not report an error , To solve this problem , We can add const
const char* p="abcdef";
Part2:
Common mistakes Compare character arrays Constant space
We often encounter the judgment condition of comparing two arrays when we are doing problems , At this time, if you directly compare array names , That's actually comparing addresses .
char arr[] = "abcdef";
char arr1[] = "abcdef";
if(arr==arr1)// The address is different
Constant string , If two constant strings are the same , Then the system will not open up two spaces for storage , At this time, if you point to it with two pointers , Its storage address is the same .
char* p = "abcdef";
char* q = "abcdef";
if(p==q)// Because constants are the same ,p And q The value of is the same
边栏推荐
- 【二叉搜索树】增删改查功能代码实现
- Cloudcompare & PCL point cloud randomly adds noise
- wx. Getlocation (object object) application method, latest version
- Doppler effect (Doppler shift)
- 建立时间和保持时间的模型分析
- JS can really prohibit constant modification this time!
- Room cannot create an SQLite connection to verify the queries
- Date类中日期转成指定字符串出现的问题及解决方法
- 7.5 装饰器
- XML configuration file (DTD detailed explanation)
猜你喜欢
Knowledge about the memory size occupied by the structure
OS i/o devices and device controllers
教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!
如何解决ecology9.0执行导入流程流程产生的问题
认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
数据库遇到的问题
提升工作效率工具:SQL批量生成工具思想
时间戳的拓展及应用实例
Key structure of ffmpeg - avformatcontext
Doppler effect (Doppler shift)
随机推荐
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
Priority queue (heap)
Mysql - CRUD
认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
Mathematical model Lotka Volterra
[designmode] Decorator Pattern
2022.7.5-----leetcode.729
MySQL functions
Tools to improve work efficiency: the idea of SQL batch generation tools
Miaochai Weekly - 8
Atcoder beginer contest 254 [VP record]
18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
What are Yunna's fixed asset management systems?
PHP determines whether an array contains the value of another array
关于slmgr命令的那些事
第16章 OAuth2AuthorizationRequestRedirectWebFilter源码解析
Transport layer protocol ----- UDP protocol
硬件及接口学习总结