如何避免在C常量的值变化 [英] how to avoid changing value of const in C

查看:85
本文介绍了如何避免在C常量的值变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是一个初学者到编程世界,
我想知道如何避免改变一个const变量的值。

Hi, I am a beginner to the programming world, i want to know how to avoid changing the value of a const variable.

#include <stdio.h>
int main()
{
 const int i = 10;
 int * p = &i;
 *p = 20;
 printf("*p = %d\ni = %d\n", *p,i);
 printf("%u\n%u\n",&i, p);
 return 0;
}

在这里上面的程序我收到的唯一警告,但没有错误,而我通过指针赋值价值为const领域,但是当我打印出来是没有什么不同:

here in above program i am getting only warning but no error while i am assigning value to const field via pointer, but when i print the out isn't different:

*p = 20
i = 10
3210723532
3210723532

这样的话有什么用让指针指向const时,不能够改变它的。

so then what is the use of getting pointer to const when it is not change able.

推荐答案

C标准只使用术语诊断。有警告和错误之间没有区别。

The C standard uses only the term "diagnostic". There is no distinction between warnings and errors.

const int的* 为int * 不进行强制转换的转换是违反约束,需要一个诊断。

The conversion of const int * to int * without a cast is a constraint violation that requires a diagnostic.

这把它在同一类别作为语法错误:您的code是不是一个有效的ISO C程序,如果它是无论如何翻译并执行的行为是不确定的。

This puts it in the same category as a syntax error: your code is not a valid ISO C program and the behavior is undefined if it is translated anyway and executed.

不幸的是,很多编译器不会从额外的诊断鉴别诊断需要他们补充,而更糟糕的是,他们有时需要作为诊断警告。这是允许的:C标准不说,这需要一个诊断程序必须是翻译和执行pvented $ P $。通过发出警告,编译器满足要求的诊断,这是那

Unfortunately, many compilers do not distinguish required diagnostics from extra diagnostics that they add, and what is worse, they sometimes required diagnostics as warnings. This is allowed: the C standard doesn't say that programs which require a diagnostic must be prevented from translating and executing. By emitting a warning, a compiler satisfies the requirement for diagnosis and that is that.

另一个问题是,很多C编译器甚至不接受标准ISO C方言,除非他们被要求,而是接受自己的方言,这可能不合格(即不向后兼容)扩展。

Another issue is that many C compilers do not even accept the standard ISO C dialect unless they are asked to, but instead accept their own dialect, which may have nonconforming (i.e. not backward compatible) extensions.

GNU C编译器理解一个C的方言叫做GNU C 89,如果你不给它任何方言的选项。

The GNU C compiler understands a C dialect called GNU C 89, if you don't give it any dialect options.

所以,讽刺的是,在这种方言,这仅仅是一个警告,转换 const int的* 为int * 没有一个演员,而某些法律C90程序被完全拒绝。

So, ironically, in this dialect, it is a mere warning to convert const int * to int * without a cast, whereas certain legal C90 programs are completely rejected.

所以基本上你总是要了解如何控制自己的编译器的输入方言,并跟进所有的警告,你要明白,自己,他们是否真正违规的语言,或者只是率性编译器作家(建议解决这个前pression圆括号)。

So basically you always have to understand how to control the input dialect of your compiler, and follow up on all the warnings, which you have to understand, by yourself, whether they are real violations of the language, or just the whims of the compiler writers ("suggest parentheses around this expression").

这不是一个坏主意与的gcc -Wall -W -ansi 编译(如果你在C90编程;或 -std = C99 而不是 ANSI 为C99),并确保没有警告。甚至也不是那些风格这表明额外的括号和等。如果你没有足够的纪律跟进警告添加 -Werror 来把它们变成失败的构建错误。也有人建议 -pedantic

It's not a bad idea to compile with gcc -Wall -W -ansi (if you're programming in the C90; or -std=c99 instead of ansi for C99) and ensure that there are no warnings. Not even the stylistic ones which suggest extra parentheses and such. If you're not disciplined enough to follow up on warnings add -Werror to turn them into errors that fail your build. Some would also recommend -pedantic.

如果你按照这门学科,一个 const int的颠覆不会潜入您的codeBase的。

If you follow this discipline, the subversion of a const int will not sneak into your codebase.

这篇关于如何避免在C常量的值变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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