Why the compiler gives warning ?

J

junky_fellow

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.
 
R

Robert Gamble

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
 
K

Krishanu Debnath

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
 
K

kernelxu

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

Ed Vogel

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
 
K

Keith Thompson

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.
 
E

Ed Vogel

Keith Thompson said:
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.
 
K

Keith Thompson

Ed Vogel said:
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.
 
E

Ed Vogel

Keith Thompson said:
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
 
J

Jack Klein

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.
 
P

Peter Nilsson

Jack said:

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?
 
K

kernelxu

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
 
J

junky_fellow

Ed said:
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.
 
J

junky_fellow

Keith said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top