我们如何在运行时更改常量的值? [英] How can we change the value of a constant at run time?

查看:97
本文介绍了我们如何在运行时更改常量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在运行时更改常量的值?

声明如下:

How can we change the value of a constant at run time?

The declaration is as follows:

const int a=10;



请帮忙!

:confused:



Please help!

:confused:

推荐答案

//如何在运行时更改常量的值?

嗯...请问我们的原因在哪里? :)
// How can we change the value of a constant at run time?

Hmm... Where could be a reason for us, please ? :)


我敢说如果要在程序中更改"a",则不要将其声明为const int.省略const部分,并使其成为int a;.
然后,您可以随意更改它.


干杯,


Manfred
I''d dare say if you want to change "a" in your program then don''t declare it as a const int. Leave out the const part and make that int a;.
Then you''re free to change it as much as you want.


Cheers,


Manfred


#include<stdio.h>
#include<conio.h>


void main()
{
    const int x = 5;
    int *p = (int *) &x;
    printf("x = %d\n", x);
    *p = 10;
    printf("x = %d", x);
    getch();
}


这篇关于我们如何在运行时更改常量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆