为什么编译器发出警告? [英] Why the compiler gives warning ?

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

问题描述

N869,第47页,


"除非它是sizeof运算符或一元&

运算符的操作数,或者是用于初始化数组的字符串文字,具有''''类型''''数组的

表达式转换为类型为'''的

表达式'指向类型'''的指针指向数组对象的

初始元素,而不是左值。


现在,请考虑以下一段代码,


char arr [10]; / *(第1行)* /

char(* arr_ptr)[10]; / *(第2行)* /

arr_ptr =& arr; / *(第3行)* /


& arr应该有类型,指向10个字符数组的指针。但是,在

编译中我得到以下错误。


1)在此声明中,&在数组arr之前被忽略。

2)在这个陈述中,指针的引用类型

value"& arr"是signed char,这是不兼容的

与array [3] of signed char。


为什么第3行给出上述警告? arr_ptr和& arr

都有相同的类型。

N869, Page 47,

"Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ''''array of type'''' is converted to an
expression with type ''''pointer to type'''' that points to the
initial element of the array object and is not an lvalue."

Now, consider following piece of code,

char arr[10]; /* (line 1) */
char (*arr_ptr)[10]; /* (line 2) */
arr_ptr = &arr; /* (line 3) */

&arr should have type, pointer to array of 10 chars. But, on
compilation I get the following errors.

1) In this statement, & before array "arr" is ignored.
2) In this statement, the referenced type of the pointer
value "&arr" is "signed char", which is not compatible
with "array [3] of signed char".

Why "line 3" is giving the above warnings ? arr_ptr and &arr
both have the same types.

推荐答案

ju ********** @ yahoo.co.in 写道:
N869,第47页,

"除非它是sizeof运算符或一元&
运算符的操作数,或者是用于初始化数组的字符串文字,否则<具有''''类型''''数组的表达式转换为
表达式,类型为''''指向类型'''的指针指向
初始元素数组对象并不是左值。

现在,请考虑以下代码,

char arr [10]; / *(第1行)* /
char(* arr_ptr)[10]; / *(第2行)* /
arr_ptr =& arr; / *(第3行)* /

& arr应该有类型,指向10个字符数组的指针。但是,在
编译时,我会收到以下错误。

1)在此声明中,&在数组arr之前被忽略。
2)在这个陈述中,指针的引用类型
值& arr"是signed char,它与signed char的数组[3]不兼容。

为什么第3行给出上述警告? arr_ptr和& arr
都有相同的类型。
N869, Page 47,

"Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ''''array of type'''' is converted to an
expression with type ''''pointer to type'''' that points to the
initial element of the array object and is not an lvalue."

Now, consider following piece of code,

char arr[10]; /* (line 1) */
char (*arr_ptr)[10]; /* (line 2) */
arr_ptr = &arr; /* (line 3) */

&arr should have type, pointer to array of 10 chars. But, on
compilation I get the following errors.

1) In this statement, & before array "arr" is ignored.
2) In this statement, the referenced type of the pointer
value "&arr" is "signed char", which is not compatible
with "array [3] of signed char".

Why "line 3" is giving the above warnings ? arr_ptr and &arr
both have the same types.




您使用的是pre-ansi C编译器吗?这些编译器可能产生这些类型的警告,见FAQ 6.12。


Robert Gamble



Are you using a pre-ansi C compiler? Such compilers might generate
these types of warnings, see FAQ 6.12.

Robert Gamble


ju**********@yahoo.co。在写道:
N869,第47页,

"除非它是sizeof运算符或一元&
运算符的操作数,或者是用于初始化数组的字符串文字,具有''''类型''''数组的
表达式转换为
表达式,类型为''''指向类型的指针''''指向数组对象的
初始元素,而不是左值。

现在,请考虑以下代码,

char arr [10]; / *(第1行)* /
char(* arr_ptr)[10]; / *(第2行)* /
arr_ptr =& arr; / *(第3行)* /

& arr应该有类型,指向10个字符数组的指针。但是,在
编译时,我会收到以下错误。

1)在此声明中,&在数组arr之前被忽略。
2)在这个陈述中,指针的引用类型
值& arr"是signed char,它与signed char的数组[3]不兼容。

为什么第3行给出上述警告? arr_ptr和& arr
都有相同的类型。
N869, Page 47,

"Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ''''array of type'''' is converted to an
expression with type ''''pointer to type'''' that points to the
initial element of the array object and is not an lvalue."

Now, consider following piece of code,

char arr[10]; /* (line 1) */
char (*arr_ptr)[10]; /* (line 2) */
arr_ptr = &arr; /* (line 3) */

&arr should have type, pointer to array of 10 chars. But, on
compilation I get the following errors.

1) In this statement, & before array "arr" is ignored.
2) In this statement, the referenced type of the pointer
value "&arr" is "signed char", which is not compatible
with "array [3] of signed char".

Why "line 3" is giving the above warnings ? arr_ptr and &arr
both have the same types.




您的代码完全合法。你必须使用一个破碎的

编译器。


Krishanu



Your code is perfectly legal. You must have been using a broken
compiler.

Krishanu



ju**********@yahoo.co.in 写道:
N869,第47页,

"除非它是sizeof运算符或一元&
运算符的操作数,或者是一个字符串用于初始化数组的文字,具有''''类型''''数组的
表达式转换为
表达式,类型为''''指针,类型为''''指向数组对象的初始元素,而不是左值。

现在,请考虑以下代码,

char arr [10] ; / *(第1行)* /
char(* arr_ptr)[10]; / *(第2行)* /
arr_ptr =& arr; / *(第3行)* /

& arr应该有类型,指向10个字符数组的指针。但是,在
编译时,我会收到以下错误。

1)在此声明中,&在数组arr之前被忽略。
2)在这个陈述中,指针的引用类型
值& arr"是signed char,它与signed char的数组[3]不兼容。

为什么第3行给出上述警告? arr_ptr和& arr
两者都有相同的类型。
N869, Page 47,

"Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ''''array of type'''' is converted to an
expression with type ''''pointer to type'''' that points to the
initial element of the array object and is not an lvalue."

Now, consider following piece of code,

char arr[10]; /* (line 1) */
char (*arr_ptr)[10]; /* (line 2) */
arr_ptr = &arr; /* (line 3) */

&arr should have type, pointer to array of 10 chars. But, on
compilation I get the following errors.

1) In this statement, & before array "arr" is ignored.
2) In this statement, the referenced type of the pointer
value "&arr" is "signed char", which is not compatible
with "array [3] of signed char".

Why "line 3" is giving the above warnings ? arr_ptr and &arr
both have the same types.




什么是你的编译器?

我什么也没得到DEV-C ++。

和& arr与arr具有相同的值,运行此程序


#include< stdio.h>


int main(无效)

{

char arr [6] =" hello";


printf(" arr =%d",arr);

printf("& arr =%d",& arr);


返回0;

}



What''s your compiler?
I got nothing under DEV-C++.
and, &arr has the same value with arr,run this program

#include <stdio.h>

int main(void)
{
char arr[6] = "hello";

printf("arr = %d", arr);
printf("&arr = %d", &arr);

return 0;
}


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

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