这些警告意味着什么? [英] What do these warnings mean?

查看:116
本文介绍了这些警告意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试初始化一组错误消息,以便我可以使用数组中的''第n个字符串打印出错误消息,例如


printf("%s \ n",messages [n]);


我对指向字符串的指针数组仍然很模糊,所以在检查我的代码之前,您可能想要
完成饮料。这是一个小样本

程序。


#include< stdio.h>

#include< stdlib。 h>

#include< string.h>

int main(无效)

{

char * messages [4] = {" Hell"," o"," "," world"};

printf("%s%s%s%s%s \ n",message [1],message [2],

消息[0],消息[2],消息[3]);

返回EXIT_SUCCESS;

}


在极其痛苦的ansi模式下编译gcc(-ansi -pedantic,以及

其他几个深奥的选项),它给出了...


[20:21:02] [/ misc / home / user2 / tbrg] ./test1

o Hell world


这就是我的预期。但是编译器提供了以下

警告,与char * messages [4]相关。声明...


test1.c:在函数main中:

test1.c:6:警告:初始化从指针目标类型中丢弃限定符

test1.c:6:警告:初始化从指针目标类型中丢弃限定符

test1.c:6:警告:初始化从指针目标类型中丢弃限定符

test1.c:6:警告:初始化从指针目标类型中丢弃限定符


由于gcc的警告已发现一些合法的我的吼叫已经好了,我认真对待他们。我做错了什么,我怎么能用b $ b来纠正它?


-

Walter Dnes;我的电子邮件地址是* ALMOST *,如 wz*******@waltdnes.org

删除z得到我的真实地址。如果被阻止,请按照550消息末尾的说明进行操作。

I''m trying to initialize an array of error messages, so that I can
print out an error message by using the ''nth string in an array, e.g.

printf("%s\n", messages[n]);

I''m still hazy on arrays of pointers to strings, so you may want to
finish your drinks before examining my code. Here''s a small sample
program.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *messages[4] = {"Hell", "o", " ", "world"};
printf("%s%s%s%s%s\n", messages[1], messages[2],
messages[0], messages[2], messages[3] );
return EXIT_SUCCESS;
}

Compiled under gcc in excruciatingly ansi mode (-ansi -pedantic, and a
few other esoteric options as well), it gives...

[20:21:02][/misc/home/user2/tbrg] ./test1
o Hell world

That''s what I expected. But the compiler gives the following
warnings, related to the "char *messages[4]" declaration...

test1.c: In function `main'':
test1.c:6: warning: initialization discards qualifiers from pointer target type
test1.c:6: warning: initialization discards qualifiers from pointer target type
test1.c:6: warning: initialization discards qualifiers from pointer target type
test1.c:6: warning: initialization discards qualifiers from pointer target type

Since gcc''s warnings have spotted a few "legal" howlers of mine
already, I take them seriously. What am I doing wrong, and how can I
correct it ?

--
Walter Dnes; my email address is *ALMOST* like wz*******@waltdnes.org
Delete the "z" to get my real address. If that gets blocked, follow
the instructions at the end of the 550 message.

推荐答案

[...]
[...]
这就是我的预期。但编译器提供以下
警告,与char * messages [4]相关。声明......
That''s what I expected. But the compiler gives the following
warnings, related to the "char *messages[4]" declaration...




i无法告诉你,但我想这是没有const的gcc

试图告诉你(但它仍然完全有效C,K& R至少)

尝试声明它

const char * foo [] = {" 1", 2,3,4};


C编译器必须接受

char * c =" 12345";

但你不能改变价值(不是在C89,C99)

c [0] = 2;


那个这就是为什么,首选和清晰的方式应该是

const char * c =" 12345";

c [0] = 2; //< - 错误


我的50美分

-

Daniel



i cant tell you for sure, but i suppose it is the absence of const what gcc
tries to tell you (but it still perfectly valid C, K&R at least)
try to declare it

const char * foo[] = {"1", "2", "3", "4"};

C compiler must accept
char * c = "12345";
but you are not allowed to change the values (not in C89, C99)
c[0] = 2;

that is why, the prefered and clenn way should be
const char * c ="12345";
c[0] = 2; // <--error

my 50 cent

--
Daniel


" Walter Dnes(删除''z''以获取我的真实地址)"写道:
"Walter Dnes (delete the ''z'' to get my real address)" wrote:

我正在尝试初始化一组错误消息,以便我可以通过使用数组中的''第n个字符串来打印出错误消息,例如

.... snip ... {
char * messages [4] = {" Hell"," o"," "," world"};
.... snip ...
这就是我的预期。但编译器提供以下
警告,与char * messages [4]相关。声明......

test1.c:在函数`main'':
test1.c:6:警告:初始化从指针目标类型
....中丢弃限定符剪辑...
由于gcc的警告已经发现了一些合法的警告。我的咆哮已经,我认真对待他们。我做错了什么,以及如何纠正它?

I''m trying to initialize an array of error messages, so that I can
print out an error message by using the ''nth string in an array, e.g.
.... snip ... {
char *messages[4] = {"Hell", "o", " ", "world"}; .... snip ...
That''s what I expected. But the compiler gives the following
warnings, related to the "char *messages[4]" declaration...

test1.c: In function `main'':
test1.c:6: warning: initialization discards qualifiers from pointer target type .... snip ...
Since gcc''s warnings have spotted a few "legal" howlers of mine
already, I take them seriously. What am I doing wrong, and how
can I correct it ?




将该数组定义为const char * messages [4] = ... "那些

消息是不可修改的。这么说吧。


-

计算机软件行业最惊人的成就

是它继续取消稳定而惊人的计算机硬件行业所取得的收益...... - Petroski



Define that array as "const char *messages[4] = ...." Those
messages are unmodifiable. Say so.

--
"The most amazing achievement of the computer software industry
is its continuing cancellation of the steady and staggering
gains made by the computer hardware industry..." - Petroski


文章< news:2i ************ @ uni-berlin.de>

Walter Dnes< se ***************************** @ waltdnes.org>写了

(部分):
In article <news:2i************@uni-berlin.de>
Walter Dnes <se*****************************@waltdnes.org> wrote
(in part):
char * messages [4] = {" Hell"," o"," "," world"};
在极其痛苦的ansi模式下编译gcc(-ansi -pedantic,以及其他一些其他深奥的选项),它给...


其中一个深奥可选地,选项是-Wwrite-strings。


在C中,字符串文字[%]产生数组N of char。对象,

其中N比文字中的字符数多一个

- 额外的1用于终止''\ 0''。使用

对象的值来初始化char *是完全可以的。变量:


char * p =" zorg";


匿名的类型应该是,但是对于历史

原因不是,const char的数组N。注意这里额外的const

。如果它*已经*,上面显然是简写:


/ * str00007是因为(显然)之前有6个其他的

字符串文字。 * /

static const char str00007 [] = {''z'',''o'',''r'',''g'',''\ 0''} ;

char * p =& str00007 [0];


和* this *需要诊断,例如:

警告:初始化从指针目标类型中丢弃限定符
char *messages[4] = {"Hell", "o", " ", "world"};
Compiled under gcc in excruciatingly ansi mode (-ansi -pedantic, and a
few other esoteric options as well), it gives...
One of those "esoteric" options was, presumably, "-Wwrite-strings".

In C, a string literal[%] produces an "array N of char" object,
where N is one more than the number of characters inside the literal
-- the extra 1 is for the terminating ''\0''. It is quite OK to use
the value of the object to initialize a "char *" variable:

char *p = "zorg";

The type of the anonymous "ought to have been", but for historical
reasons is not, "array N of const char". Notice the extra "const"
here. If it *had* been, the above would clearly be shorthand for:

/* str00007 is because (apparently) there were 6 other
string literals earlier. */
static const char str00007[] = { ''z'', ''o'', ''r'', ''g'', ''\0'' };
char *p = &str00007[0];

and *this* would require a diagnostic such as:
warning: initialization discards qualifiers from pointer target type



因为& str00007 [0]是const char *,但是p只是普通的char *"

- 缺少const - 限定符。


现在,ANSI C中字符串文字的特殊之处在于,它们

真的*是*只读 - 如果你写一个,效果是

undefined,和一个好的诸如BSD或Linux之类的系统会给运行时陷阱带来
。然而,尽管它们是只读的,并且

" const"关键字*表示*只读,它们没有const

属性。这是为了历史兼容性:在1989年
,小心不要写在只读对象上。因此,世界

充满了(非常好的)C代码,它不会*使用*const。


GCC'' -Wwrite串" flag告诉gcc你,程序员,

想要缺少的const陷入困境。如果你这样做,你将获得

警告,不需要任何警告。可以使用普通的

" char *"指向一个字符串文字,这样的代码确实存在,

所以这个警告可能很烦人。不过,你必须要小心

不要写上它:


char * p =" zorg";


罚款;你只需要确保你没有,稍后,尝试做

之类的事情:


p [0] =''Z''; / * oops,应该是大写的* /


如果你使p具有类型const char *,编译器将能够

在编译时捕获这个错误,代码将使用和不使用-Wwrite-strings来干净地编译




GCC''可选的警告给你一个选择:警告我失踪

`const''所以我不会忘记并尝试通过指针写,

或不要警告我这件事,因为如果我不忘记那就没关系,

并且警告很烦人。您可以自行决定使用这些

中的哪一个。

-

In-Real-Life:Chris Torek,Wind River Systems

美国犹他州盐湖城(40°39.22''N,111°50.29''W)+1 801 277 2603

电子邮件:忘了它 http://web.torek.net/torek/index.html

由于垃圾邮件发送者,阅读电子邮件就像在垃圾中搜索食物一样。



because &str00007[0] is "const char *", but p is just plain "char *"
-- the "const"-qualifier is missing.

Now, the peculiar thing about string literals in ANSI C is, they
really *are* read-only -- if you write on one, the effect is
undefined, and a "good" system such as a BSD or Linux will give
you a runtime trap. Yet, even though they are read-only, and the
"const" keyword *means* read-only, they do not have the "const"
attribute. This is for historical compatibility: before the 1989
C standard, there *was* no "const" keyword. Programmers just had
to be careful not to write on read-only objects. Hence, the world
is full of (perfectly good) C code that does not *use* "const".

GCC''s "-Wwrite-strings" flag tells gcc that you, the programmer,
want the missing "const" stuck in. If you do this, you will get
warnings where none are required. It is quite OK to use a plain
"char *" to point at a string literal, and such code does exist,
so this warning could be annoying. Still, you have to be careful
not to write on it:

char *p = "zorg";

is fine; you just have to make sure you do not, later, try to do
something like:

p[0] = ''Z''; /* oops, was supposed to be uppercase */

If you make p have type "const char *", the compiler will be able
to catch this mistake at compile-time, and the code will compile
cleanly both with and without "-Wwrite-strings".

GCC''s optional warning gives you a choice: "warn me about missing
`const''s so I do not forget and try to write through the pointer",
or "do not warn me about this, because it is OK if I do not forget,
and the warning is annoying". You get to decide which of these
holds true for your own code.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


这篇关于这些警告意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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