当前位置:网站首页>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
边栏推荐
- Search (DFS and BFS)
- [Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
- Solve the problem of reading Chinese garbled code in sqlserver connection database
- Ffmpeg learning - core module
- What is information security? What is included? What is the difference with network security?
- Date类中日期转成指定字符串出现的问题及解决方法
- 【在线聊天】原来微信小程序也能回复Facebook主页消息!
- 从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
- 提升工作效率工具:SQL批量生成工具思想
- 2022.7.5-----leetcode.729
猜你喜欢
Miaochai Weekly - 8
How to solve the problems caused by the import process of ecology9.0
Priority queue (heap)
Notepad + + regular expression replace String
Huawei equipment configuration ospf-bgp linkage
【在线聊天】原来微信小程序也能回复Facebook主页消息!
剖面测量之提取剖面数据
GD32F4xx uIP协议栈移植记录
Classical concurrency problem: the dining problem of philosophers
教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!
随机推荐
Mathematical model Lotka Volterra
Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
云呐|公司固定资产管理系统有哪些?
Learn PWN from CTF wiki - ret2libc1
Room cannot create an SQLite connection to verify the queries
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
wx. Getlocation (object object) application method, latest version
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
How to solve the problems caused by the import process of ecology9.0
Search (DFS and BFS)
[gym 102832h] [template] combination lock (bipartite game)
FFMPEG关键结构体——AVFrame
LeetCode 8. String conversion integer (ATOI)
What are Yunna's fixed asset management systems?
[Luogu cf487e] tours (square tree) (tree chain dissection) (line segment tree)
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
MySQL之函数
Hudi of data Lake (1): introduction to Hudi
Cloudcompare & PCL point cloud randomly adds noise
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share