当前位置:网站首页>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
边栏推荐
- Room cannot create an SQLite connection to verify the queries
- 单商户V4.4,初心未变,实力依旧!
- PV static creation and dynamic creation
- wx. Getlocation (object object) application method, latest version
- 时间戳的拓展及应用实例
- Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
- Learn PWN from CTF wiki - ret2libc1
- LeetCode 1189. Maximum number of "balloons"
- Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
- Atcoder beginer contest 258 [competition record]
猜你喜欢
MDK debug时设置数据实时更新
Date类中日期转成指定字符串出现的问题及解决方法
剖面测量之提取剖面数据
FFMPEG关键结构体——AVFrame
Key structure of ffmpeg -- AVCodecContext
PV static creation and dynamic creation
建立时间和保持时间的模型分析
Gd32f4xx UIP protocol stack migration record
Knowledge about the memory size occupied by the structure
Classical concurrency problem: the dining problem of philosophers
随机推荐
Senparc. Weixin. Sample. MP source code analysis
Key structure of ffmpeg - avframe
7.5 simulation summary
Add noise randomly to open3d point cloud
JS can really prohibit constant modification this time!
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Atcoder beginer contest 254 [VP record]
Room cannot create an SQLite connection to verify the queries
Zhuan: in the future, such an organization can withstand the risks
数据库遇到的问题
Classical concurrency problem: the dining problem of philosophers
Go learning --- read INI file
Ffmpeg learning - core module
PV static creation and dynamic creation
[Luogu p3295] mengmengda (parallel search) (double)
MySQL之函数
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
Hudi of data Lake (1): introduction to Hudi
MySql——CRUD