C中被const了的类型可以改变吗? - momodi's Blog

C中被const了的类型可以改变吗?

momodi posted @ Tue, 02 Feb 2010 09:18:39 +0800 in IProgramming with tags The C Programming Language , 1102 readers

  c语言真的是有很多很有趣的地方。现在说的这个问题,是我无意中发现的。现在回想起来,觉得挺有意思的,在这里记录下来。

 且看如下代码:

const int a = 123;
a = 456

 

这个代码肯定都能看得出来,编译是不会通过的。

但是我想达到让变量a == 456有什么方法可以吗?

C语言既然号称是自由的语言,我们就有绕过他语法限制的方法咯。

看了如下的代码,你会不会有所疑惑。

 

这个代码在g++中是可以编译通过的,也就是说我们绕过了语法限制,使得a的值得到了改变。但结果真的是这样的吗?答案是否定的,当你运行这个程序的时候,结果还是123。为了表现这个东西神奇,再看如下代码:

 

int main() {
    const int a = 123;
    int *b = (int*)&a;
    *b = 456;
    printf("%x: %d\n", &a, a);
    printf("%x: %d\n", b, *b);
    return 0;
}

 

这个程序在这本机是这样输出的:

 

 

28ff38: 123

28ff38: 456

 

 

这个程序就是输出了两个变量的地址和值。但是从结果上来显示,他们在内存中的地址是一样的,但是值却不一样!奇怪否?我刚接触这个问题的时候,也觉得相当的奇怪,后来我猜测是编译器优化搞的鬼。

为了验证这个问题,我用如下代码测试了一下:

 

int main() {
    int c = 123;
    const int a = c;
    int *b = (int*)&a;
    *b = 456;
    printf("%x: %d\n", &a, a);
    printf("%x: %d\n", b, *b);
    return 0;
}

 

这下结果终于对了:)

 

28ff34: 456

28ff34: 456

 

 

 

Avatar_small
meidir said:
Sat, 27 Aug 2022 19:58:04 +0800

Thanks for taking the time to talk about this, Personally i think strongly about it and love learning more with this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is very ideal for me. bolsas ecologicas de tela

Avatar_small
meidir said:
Mon, 29 Aug 2022 22:22:54 +0800

We have not too long ago started out any website, the knowledge an individual offer on this web site provides aided myself tremendously. Thank you regarding your entire moment & perform. 手機回收

 

==================

 

her in fact awesome blog page. her realy content rich and then a this fantastic profession. i prefer this unique. 電腦回收

 

====================

 

Freezing consideration perhaps it is an understanding to create could possibly help everybody else is having troubles searching though My group is some unclear a lot more i am permitted to get details and even communications information at these. 攝影器材

 

=================

 

A totally free tell you which usually they can be a a superb content from the awesome people, we're very happy to watch this approach. 補光燈

Avatar_small
meidir said:
Wed, 31 Aug 2022 19:56:05 +0800

Wonderful posting, Thanks quite a bit to obtain spreading The following awareness. Wonderfully authored posting, doubts all of blog owners available precisely the same an increased standard subject material just like you, online has to be improved site. I highly recommend you stay the best! Thank you flowers and gifts

Avatar_small
meidir said:
Mon, 05 Sep 2022 19:00:13 +0800

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. sell house fast seattle

Avatar_small
meidir said:
Mon, 05 Sep 2022 19:00:35 +0800

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. window tint sacramento

Avatar_small
meidir said:
Mon, 05 Sep 2022 19:00:51 +0800

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. seattle flower delivery

Avatar_small
meidir said:
Mon, 05 Sep 2022 19:01:15 +0800

I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. 5 fold ministry gifts

Avatar_small
meidir said:
Mon, 05 Sep 2022 19:01:45 +0800

This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again. How old is god now

Avatar_small
meidir said:
Mon, 05 Sep 2022 19:02:07 +0800

Thank you for very usefull information.. Bounce house rental

Avatar_small
meidir said:
Mon, 05 Sep 2022 19:02:22 +0800

i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to present something back and help others such as you helped me. How much does a bounce house cost

Avatar_small
meidir said:
Mon, 05 Sep 2022 19:02:42 +0800

I am glad you take pride in what you write. This makes you stand way out from many other writers that push poorly written content. Chair rentals sacramento

Avatar_small
meidir said:
Mon, 05 Sep 2022 19:03:20 +0800

I am jovial you take pride in what you write. It makes you stand way out from many other writers that can not push high-quality content like you. Water damage Spokane

Avatar_small
meidir said:
Wed, 07 Sep 2022 16:37:20 +0800

Thank you a lot for sharing this with all of us you really know what you are speaking approximately! Bookmarked. Kindly also consult with my web site =). We may have a hyperlink trade contract between us! is god real


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter