473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why the compiler gives warning ?

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.

Nov 15 '05 #1
13 1310
ju**********@yahoo.co.in wrote:
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.


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

Robert Gamble

Nov 15 '05 #2

ju**********@yahoo.co.in wrote:
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.


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

Krishanu

Nov 15 '05 #3

ju**********@yahoo.co.in wrote:
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.


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;
}

Nov 15 '05 #4

<ju**********@yahoo.co.in> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.


Based on the warning message, I assume you are using
the DEC/Compaq/HP Ccompiler on Tru64 or OpenVMS.
If this is not correct...ignore the rest of this reply.

You are getting these messages because you are invoking
the compiler in its "K & R" mode. Some early C compilers
ignored the & operator when it was used on an array.
You specified K & R mode by either using the -std0
switch, or you are using a very old version of the compiler
where -std0 was the default behavior.

If you specify -std, the compiler will use it's "Relaxed" Standard
mode, and should compile the program as you expect.

Ed Vogel
HP/Compaq/DEC C/C++ Engineering
Nov 15 '05 #5
ju**********@yahoo.co.in writes:
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.


First of all, I don't think the message '& before array "arr" is
ignored' is an error message; it looks more like a warning.

Was that the actual code you compiled? The error message refers to
"signed char", but all your declarations use plain "char". (Possibly
a compiler might (incorrectly) treat char and signed char as the same
type.) And it refers to "array [3] of signed char"; I don't see a
3-element array of *anything* in your code fragment.

You know the drill. Post the *exact* code you compiled (cut-and-paste
the entire small compilable program) and the *exact* messages you got
from the compiler (cut-and-paste again). Otherwise, we can't guess
which errors are in your original code and which were introduced when
you re-typed it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Was that the actual code you compiled? The error message refers to
"signed char", but all your declarations use plain "char". (Possibly
a compiler might (incorrectly) treat char and signed char as the same
type.) erefore, we must do this.


Unless that compiler was told to compile the program in K & R mode
(where "char" and "signed char" could be treated the same as there
was no "signed char" in K & R C) as I speculated in an earlier reply.

Ed Vogel
HP/Compaq/DEC C/C++ Engineering.

Nov 15 '05 #7
"Ed Vogel" <ed************************@hp.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Was that the actual code you compiled? The error message refers to
"signed char", but all your declarations use plain "char". (Possibly
a compiler might (incorrectly) treat char and signed char as the same
type.) erefore, we must do this.


Unless that compiler was told to compile the program in K & R mode
(where "char" and "signed char" could be treated the same as there
was no "signed char" in K & R C) as I speculated in an earlier reply.


You're right, the compiler does refer to "signed char" when I invoke
it with "cc -std0" (K&R mode) -- but it still doesn't refer to a
3-element array when the source declares a 10-element array.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
You're right, the compiler does refer to "signed char" when I invoke
it with "cc -std0" (K&R mode) -- but it still doesn't refer to a
3-element array when the source declares a 10-element array.


Keith,

When I try the program here, the message refers to a 10 element
array. As you pointed out, we need the real code posted.

Thanks!

Ed
Nov 15 '05 #9
On 9 Aug 2005 05:43:56 -0700, ke******@hotmail.com wrote in
comp.lang.c:

ju**********@yahoo.co.in wrote:
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.
What's your compiler?
I got nothing under DEV-C++.
and, &arr has the same value with arr,run this program


No, don't tell anyone to run your program. It contains too many
instances of undefined behavior.
#include <stdio.h>

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

printf("arr = %d", arr);
A conversion specifier of "%d" requires an argument of signed int.
Passing it a pointer to char produces undefined behavior.
printf("&arr = %d", &arr);
A conversion specifier of "%d" requires an argument of signed int.
Passing it a pointer to an array of 6 chars produces undefined
behavior.
return 0;
}


Now let's fix your program to make it legal, defined, valid C:

#include <stdio.h>

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

printf("arr = %p", arr);
printf("&arr = %p", (void *)&arr);

return 0;
}

Note the use of the "%p" conversion specifier, which is defined for an
argument of type pointer to void. In the cast of 'arr', which is
converted to a pointer to char in this usage, no cast is necessary
because pointer to any of the character types has the same
representation and is passed to functions identically as pointer to
void. The cast is necessary when passing '&arr', which has the type
"pointer to array of six characters".

Note also that, converted to the same type, 'arr' and '&arr' will
contain the same address, but do not have the same type.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #10
Jack Klein wrote:
ke******@hotmail.com wrote in comp.lang.c:
<snip>
No, don't tell anyone to run your program. It contains too many
instances of undefined behavior.
#include <stdio.h>

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

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


A conversion specifier of "%d" requires an argument of signed int.
Passing it a pointer to char produces undefined behavior.
printf("&arr = %d", &arr);


A conversion specifier of "%d" requires an argument of signed int.
Passing it a pointer to an array of 6 chars produces undefined
behavior.
return 0;
}


Now let's fix your program to make it legal, defined, valid C:


It may be valid, but without a trailing newline on the last line of
output, its behaviour is not quite 'valid'.
#include <stdio.h>

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

printf("arr = %p", arr);
printf("&arr = %p", (void *)&arr);

return 0;
}

Note the use of the "%p" conversion specifier, which is defined for an
argument of type pointer to void. In the cast of 'arr', which is
converted to a pointer to char in this usage, no cast is necessary
because pointer to any of the character types has the same
representation
True.
and is passed to functions identically as pointer to void.


Is there normative chapter and verse for the this?

--
Peter

Nov 15 '05 #11
hello,Jack Klein,

I appreciate your words so much!

I wrote like that for a long time , but nothing wrong happened.

Indeed my programs doesn't match the standard ANSI C , I will pay more
attention.

thank you very much

Nov 15 '05 #12

Ed Vogel wrote:
<ju**********@yahoo.co.in> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.


Based on the warning message, I assume you are using
the DEC/Compaq/HP Ccompiler on Tru64 or OpenVMS.
If this is not correct...ignore the rest of this reply.

You are getting these messages because you are invoking
the compiler in its "K & R" mode. Some early C compilers
ignored the & operator when it was used on an array.
You specified K & R mode by either using the -std0
switch, or you are using a very old version of the compiler
where -std0 was the default behavior.

If you specify -std, the compiler will use it's "Relaxed" Standard
mode, and should compile the program as you expect.

Ed Vogel
HP/Compaq/DEC C/C++ Engineering


You are right. On specifying -std it works fine. Thanx for the
help.

Nov 15 '05 #13

Keith Thompson wrote:
"Ed Vogel" <ed************************@hp.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
Was that the actual code you compiled? The error message refers to
"signed char", but all your declarations use plain "char". (Possibly
a compiler might (incorrectly) treat char and signed char as the same
type.) erefore, we must do this.


Unless that compiler was told to compile the program in K & R mode
(where "char" and "signed char" could be treated the same as there
was no "signed char" in K & R C) as I speculated in an earlier reply.


You're right, the compiler does refer to "signed char" when I invoke
it with "cc -std0" (K&R mode) -- but it still doesn't refer to a
3-element array when the source declares a 10-element array.


I am sorry for posting the different code rather than the actual code.
In the original code it was declared as an array of 3 elements.
Thanx a lot for your help.

Nov 15 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Alf P. Steinbach | last post by:
// As usual the error message directs one to the report the bug. // // And as usual there is absolutely no way to do so without paying for // the privilege... // // Or using three or four hours...
5
by: Adrian | last post by:
Hi all, I am getting a warning compiling the following code using Borland C++ Builder 6, but I dont think I am doing anything wrong. When using g++ I get no warnings at all with a g++ -Wall...
20
by: Markus Sandheide | last post by:
Hello! Execute these lines: int x = 1; x = x > 2345678901; You will get: x == 1 with Borland C++ Builder
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
5
by: Ondrej Spanel | last post by:
I though that inline functions should be always visible only in the compilation unit where they are defined - meaning if compiler cannot inline them, they should be handled as if declared static....
5
by: Alan Cobb | last post by:
Hi, In the managed C++ class below I get compile warning C4677 from VS2003. "signature of non-private function contains assembly private type", even though the managed enum is public. I have...
16
by: cyber citizen | last post by:
Hi Folks, We are encountering the following code issue on compiler susch as "xlc","gcc" but "icc" passes it successfully. Sample code: int main(void) { typedef unsigned char oratext;...
11
by: zeppe | last post by:
Hi all, I've a problem. The code that follows creates a warning in both gcc and visual c++. However, I think it's correct: basically, there is a function that return an object of a derived...
244
by: Ajinkya | last post by:
Can anyone suggest me a good compiler for(c/cpp) for windows? I tried dev cpp but its debugging facility is very poor.
2
by: Chris Peters | last post by:
Hi, I'm using some Fortran 77 code together with C, and the compiler is giving me some strange warnings. Can anyone help explain what's going on? The code runs just fine. 1) Fortran code
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.