当前位置:网站首页>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

边栏推荐
- [BGP] small scale experiment
- 企业网的三层架构
- Deep understanding of Base64 underlying principles
- Ribbon principle analysis namedcontextfactory
- With more than 5 years of work experience and a salary of 15K, would you accept it if you were me?
- 数据挖掘——关联分析基础介绍(上)
- Getting started with caspin
- MySQL第三篇
- Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization
- UCOS task switching process
猜你喜欢

Shopify seller: EDM marketing should be combined with salesmartly to easily get the conversion rate
![[原理] 横向渗透的几种方式](/img/fc/2ef7dd6ebc5c0bd8f7d302d8b596d6.png)
[原理] 横向渗透的几种方式

3.解决Pycharm报错Unresolved reference ‘selenium‘ Unresolved reference ‘webdriver‘

SQL window function

RHCE的at,crontab的基本操作,chrony服务和对称加密和非对称加密

@Configuration (proxybeanmethods = false) what's the use of setting this to false

Typescript from getting started to mastering (XVI) configuration file - first knowledge of compileroptions configuration item

Alibaba Font Icon Library Usage and update methods
![[principle] several ways of horizontal penetration](/img/fc/2ef7dd6ebc5c0bd8f7d302d8b596d6.png)
[principle] several ways of horizontal penetration

EMD empirical mode decomposition
随机推荐
LDP --- 标签分发协议
基于STM32和阿里云的环境检测系统设计
VScode连接ssh遇到的问题
Analysis of new retail o2o e-commerce model
Shutter start white screen
LDP -- label distribution protocol
Data too long for column 'xxx' at row 1 solution
How fast does it take to implement a super simple programming language?
【redis系列】字符串数据结构
Opensql quick learning
Configmap配置与Secret加密
1985-2020 (8 Editions) global surface coverage download and introduction
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
First knowledge of C language (3)
Interview essential! TCP classic 15 consecutive questions!
After I get the winfrom specific control ID from the database, I need to find the corresponding control through this ID and assign a value to the text text of the control. What should I do
LVS+KeepAlived高可用部署实战应用
The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
Common methods of lodash Library
[principle] several ways of horizontal penetration