Why the compiler gives warning ?

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Aug 9, 2005.

  1. Guest

    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.
     
    , Aug 9, 2005
    #1
    1. Advertising

  2. 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
     
    Robert Gamble, Aug 9, 2005
    #2
    1. Advertising

  3. 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
     
    Krishanu Debnath, Aug 9, 2005
    #3
  4. Guest

    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;
    }
     
    , Aug 9, 2005
    #4
  5. Ed Vogel Guest

    <> wrote in message
    news:...
    > 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
     
    Ed Vogel, Aug 9, 2005
    #5
  6. 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) <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.
     
    Keith Thompson, Aug 9, 2005
    #6
  7. Ed Vogel Guest

    "Keith Thompson" <> wrote in message
    news:...
    > 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.
     
    Ed Vogel, Aug 9, 2005
    #7
  8. "Ed Vogel" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> 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) <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.
     
    Keith Thompson, Aug 9, 2005
    #8
  9. Ed Vogel Guest

    "Keith Thompson" <> wrote in message
    news:...
    > 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
     
    Ed Vogel, Aug 9, 2005
    #9
  10. Jack Klein Guest

    On 9 Aug 2005 05:43:56 -0700, wrote in
    comp.lang.c:

    >
    > 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Aug 10, 2005
    #10
  11. Jack Klein wrote:
    > 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
     
    Peter Nilsson, Aug 10, 2005
    #11
  12. Guest

    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
     
    , Aug 10, 2005
    #12
  13. Guest

    Ed Vogel wrote:
    > <> wrote in message
    > news:...
    > > 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.
     
    , Aug 10, 2005
    #13
  14. Guest

    Keith Thompson wrote:
    > "Ed Vogel" <> writes:
    > > "Keith Thompson" <> wrote in message
    > > news:...
    > >> 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.
     
    , Aug 10, 2005
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,263
    Smokey Grindel
    Dec 2, 2006
  2. Harry Ebbers

    QA-C gives side-effects warning. Unknown why

    Harry Ebbers, Nov 26, 2008, in forum: C Programming
    Replies:
    24
    Views:
    1,276
    Harry Ebbers
    Dec 1, 2008
  3. Borked Pseudo Mailed

    QA-C gives side-effects warning. Unknown why

    Borked Pseudo Mailed, Nov 29, 2008, in forum: C Programming
    Replies:
    2
    Views:
    332
    CBFalconer
    Dec 1, 2008
  4. Nomen Nescio

    QA-C gives side-effects warning. Unknown why

    Nomen Nescio, Dec 2, 2008, in forum: C Programming
    Replies:
    0
    Views:
    274
    Nomen Nescio
    Dec 2, 2008
  5. Replies:
    4
    Views:
    593
    Barry Schwarz
    Jul 26, 2012
Loading...

Share This Page