当前位置:网站首页>Const read only variable constant
Const read only variable constant
2022-07-29 04:00:00 【Xiaowa 123】
The reason for this problem , It's because I've been watching C Memory partition of various variables in language runtime , I'm not sure const Decorated variables and string constants / Whether these quantities such as numerical constants are stored together .
A read-only variable :const Decorated variable , To be exact, it is a read-only variable , It is actually readable and writable , Only the implementation of writing needs the help of pointers , Stored in the stack area , If it is defined inside a function .
Constant :“hello” This kind of is the real constant , Constant storage area stored in static memory , Managed by the system .
#include <stdio.h>
#include <string.h>
int main(void)
{
int n = 2;
int* pn = &n;
printf("int n address is %d\n", pn);
printf("\n");
const int i = 3;
//i It's a read-only variable , But it's still a variable , Stored in the dynamic storage area of stack , The compiler is responsible for
int* p = &i;//i After all, it is a variable , After initialization, it cannot be changed by direct assignment i Value , Because I added
//const,C Language grammar does not allow direct assignment . Its value can be changed through the pointer ,const It's a limited repair
// Embellishment , Don't change i It is still the nature of a variable .
*p = 200;// Modify by pointer const int i The numerical
printf("const int i aggress is %d\n", p);
printf("const int i is %d\n", i);
printf("\n");
char* c = "helloworld";// This is the real constant , Is managed by the system , The compiler cannot control .
printf(" helloworld address is %d\n", c);
return 0;
}Output results :
int n address is 15988844
const int i aggress is 15988856 // You can see const int i and int n Is stored in an area , Stack area .
const int i is 200 // Modified by pointer const int i The numerical
helloworld address is 8215608// String constants are stored in another area , That is, the constant storage area in the static storage area . The system is responsible for .
Details refer to https://blog.csdn.net/csdn_lsd/article/details/78419203
Look at all the above , Basically, I can convince myself , original const Modified change ! The amount ! It can also be modified , Now let's look at this sentence :
const It's a language feature , Cannot pass const Modify the name to modify the corresponding content , But it is not impossible to modify . Because when the compiler is implemented , There is no provision to put it in the read-only area . for instance :
int a = 5;
const int* b = &a;
You can't pass at this time b Change address &a The value in , But you can still put a Reassign . Address &a The value stored in is changed .
You might wonder , Constant pointer , constant pointer , Later, you can modify it with a pointer const Limit the value of the , Already dizzy . Actually , Don't worry too much , Why can the pointer be modified later , It can't be modified for a while , You can understand that this is C Grammar of language , The reason why it looks a little complicated , Because we need to set more rules to meet various needs .
//================const The amount of modification is still a variable , Cannot be used to define an array =========
When defining an array , The array size must be specified clearly , Or let the compiler get the array size by initializing the array elements , Such as int a[5]; char a[]={'a','b','c'}, You cannot use variables as the size of an array . The following example can illustrate const The amount of modification , Although we used to call it a constant , But in fact, it is only a read-only variable , It's readable and writable , Write through the pointer , The reason is that , This is it. const Limiting effect on variables , Just remember .
Let's take an example , explain const The amount of modification is still a variable but not a variable , It cannot be used to define array size .const The modified quantity is a read-only variable , It is still stored in the stack of dynamic storage area .
#include<stdio.h>
#include<string.h>
//#define MAX 3
int main(int argc,char* agrv[])
{
const int max = 3;
int a[max] = { 1, 2, 3 };
There is an error : The array size should be a constant value , The size cannot be defined as 0 Array of .
This error explains max It's not a constant , that max What is it? ? Use const Decorated read-only variable , What is a constant :
int a[3]={1,2,3};// Numbers 3 Is the real constant , It is unalterable , Put it in the system storage area , The compiler is not authorized to manage .
return 0;
}
Change to the following code , With predefined define Can introduce a real constant , The following code is correct .
#include<stdio.h>
#include<string.h>
#define MAX 3
int main(int argc,char* agrv[])
{
int a[MAX] = { 1, 2, 3 };//MAX It means 3 This number , Is the real constant
return 0;
}Make it clear again :const The amount of modification is not really a constant , Is a read-only variable , Store in the stack , You can modify the value through the address . Definition of array size , Be sure to use constants , This constant is a real constant , It's an unalterable number , Or unknown size , Initialize elements to define arrays .
边栏推荐
- Malloc C language
- Simple cases of inner connection and left connection
- The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
- Flutter 启动白屏
- 【C语言入门】ZZULIOJ 1031-1035
- Typescript from getting started to mastering (XXII) namespace namespace (I)
- I. creation and constraint of MySQL table
- Lvs+keepalived high availability deployment practical application
- The data type of symbol, a new feature of ES6
- Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
猜你喜欢

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

初识C语言(3)

Solve the problem of garbled code when opening the project code in idea

CUB_200鸟类数据集关键点可视化

Configmap configuration and secret encryption

Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds

EMD 经验模态分解

OA项目之会议通知(查询&是否参会&反馈详情)

LDP -- label distribution protocol

Casbin入门
随机推荐
Typescript from getting started to mastering (XX) function generics
Lvs+keepalived high availability deployment practical application
What you see and think in Microsoft
Typescript from entry to mastery (XVIII) joint type and type protection
Basic configuration of BGP - establish peers and route announcements
Several cases of word wrapping in div
Code ~ hide or disable the status bar and virtual keys
Why does the 20 bit address bus determine the storage space of 1MB
JS cookie usage
The data type of symbol, a new feature of ES6
3. Solve pychart's error unresolved reference 'selenium' unresolved reference 'webdriver‘
安装ros的laser_scan_matche库所遇到的问题(一)
1985-2020 (8 Editions) global surface coverage download and introduction
1985-2020(8个版次)全球地表覆盖下载与介绍
【深度学习CPU(番外篇)——虚拟内存】
How to write SQL statements about field conversion
SQL window function
Why don't programmers work blindly?
LDP -- label distribution protocol
JS realizes the function of one click Copy