当前位置:网站首页>Const char* and char*, string constants
Const char* and char*, string constants
2022-07-29 04:01:00 【Xiaowa 123】
notes : When it comes to strings , Basically string constants .
See such a sentence : mention const char*, Think that this pointer points to a string .
According to the previous understanding of constant pointer and pointer constant ,const char* p, That is, the pointer points to a constant , The value cannot be modified by a pointer . That is to say *p=XXX, Is the wrong usage .
Why? const char* It is used for strings , Because this is a constant pointer , Strings are constants , And it is stored in the constant storage area managed by the system , The compiler is not authorized to modify it . And again char Pointer to type , Or a constant pointer , Naturally, it can be associated with strings .
Another question :const char* and char* What's the difference :
Please see the following procedure for details :
#include<stdio.h>
#include<string.h>
int main(int argc,char* agrv[])
{
printf("1. So far, normal execution ");
const char* c = "abc";// It means that the value of the string cannot be modified through the pointer
char* s = "abc";
Pay attention to the following explanation :
s Is a string constant “abc” The pointer to , Let's see if it can be modified “abc”?
It should not be . because “abc” Is a constant , Put it in the constant storage area , This memory
The compiler has no rights to manage , It is managed by the system ,“abc” It cannot be modified by pointer .
As can be seen from the above , When targeting string variables ,const char* s =“abc” and char* s=“abc” It's essentially the same thing ,
Are pointers to constant storage areas managed by the system . although There is no error in trying to change the syntax of the string content through the constant string pointer , But in fact, the program can't run ,
This explanation , The program is compiled , It doesn't mean there's no problem when it runs .
about const char* s, This is more standardized , The compiler can directly check syntax errors .
So if you need to define a constant that points to a string ( Form like “abc”, Or single character “a”, Such character constants ) The pointer of , The standard writing is const char* The form of the variable name .
It's written in char* Variable names can also , It's just like const char* The variable name is the same , however const char* Variable name Is a more standardized way of expression .
printf("%p\n", s);
*(s+1) = "d";// Try to modify by pointer “abc” The elements in , There is no error in the statement ,
But the program can no longer run at this stage , Because this operation is illegal , Although there is no grammatical error .
printf("2. So far, normal execution ");
printf("%s\n", s); The program can't run , No output
*(c + 1) = "d"; Prompt syntax error directly , The program cannot be compiled without commenting it out
printf("%s\n", *c);
return 0;
}
From the above analysis, we can see that , When pointing to a string ( Constant ) when ,const char* and char* There is no difference , however const char* It can pass more clearly const Remind you that you cannot modify constant strings through pointers ,char* Nor can string constants be modified , But what is not shown is written const, Both are the same in use .
about char*, If you want to modify string constants only , There is no mistake in grammar , Compilation can also pass , But there will be problems when the program runs .
Reference resources const char* str="abcd" And char* str="abcd" Is there a difference ?-CSDN Forum
边栏推荐
- MySQL第四篇(完结)
- 初识C语言(3)
- Cannot paste multiple pictures at once
- 数据挖掘——关联分析基础介绍(上)
- Basic configuration of BGP - establish peers and route announcements
- Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
- Connection broken by 'readtimc rt-443): read timed out (read timeout=l5)“)‘: /pac
- The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
- 代码 ~ 隐藏或禁用状态栏和虚拟按键
- 小马智行进军前装量产,从自研域控制器入手?
猜你喜欢
Malloc C language
Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization
BGP的基础配置---建立对等体、路由宣告
SQL window function
RHCE's at, crontab's basic operations, the Chrony service, symmetric encryption and asymmetric encryption
Beijing post network research 2015 problem2
Nacos registry
How to understand "page storage management scheme"
1985-2020(8个版次)全球地表覆盖下载与介绍
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
随机推荐
Lvs+keepalived high availability deployment practical application
SSL==证书相关概念
Ribbon principle analysis namedcontextfactory
1985-2020 (8 Editions) global surface coverage download and introduction
CUB_200鸟类数据集关键点可视化
LVS+KeepAlived高可用部署实战应用
Opensql quick learning
MySQL第三篇
Form verification of landline
第一个ALV程序2
关于双指针的思想总结
Alibaba Font Icon Library Usage and update methods
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
[untitled]
基于STM32和阿里云的环境检测系统设计
Shutter start white screen
C declaration and initialization and assignment
Malloc C language
力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
关于ALV格式控制部分的写法