Why compiler warning?

This is a discussion on Why compiler warning? within the C Programming forums, part of the General Programming Boards category; My 1st post...apologies if format is incorrect. Why do I get this warning for the below simple example? "prog.c:9: warning: ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    57

    Why compiler warning?

    My 1st post...apologies if format is incorrect.

    Why do I get this warning for the below simple example?
    "prog.c:9: warning: passing arg 1 of `digital_display' from incompatible pointer type"


    Code:
    void digital_display( const char digit[10][9] )
    {
    }
    
    int main ( void )
    {
      char digit[10][9];
    
      digital_display( digit );     // this is line 9
    
    }
    This program successfully compiles if either:
    1. I take away one index: "digit[10][9];" to "digit[10];"
    or 2. take away the "const" keyword.

    Thank you.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Is your file .c or .cpp??

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    To Roaring_Tiger:

    It is prog.c

    What difference does it make?

    (also I'm running MingW gcc under Windows XP)

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Tried your program in VC 6.0 I did not get any warnings. What warning level are you at?


    I asked you for file type as C++ treat function differently.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    To Roaring_Tiger:

    I'm at whatever the default installation warning level is...I don't know.

    Is it valid C code that should not generate a warning?

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    I believe your code looks correct to me as C does not care what you pass to function. So felt like some problem with worning level.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    no. I haven't seen a warning worth ignoring. Does your compiler say you need to return something because mine does.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    Ok, it looks like a problem with gcc (an invalid warning).

    I compiled with 2 other compilers:
    The free Borland compiler gave me 2 useful valid warnings, but not the warning in question.
    The Microsoft C++.net (VC 7.0?) compiler gave me no warnings.


    Thanks for your help Roaring_Tiger!

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I doubt that. Maybe those other compilers aren't as harsh as you have gcc set to

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    To linuxdude:

    Neither the gcc compiler nor the Microsoft compiler give me that warning.
    The Borland compiler does.

    Thank you for your help!

  11. #11
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    [edit]
    never mind, still want to read this though:
    passing a 2dim array to a funct
    [/edit]
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    To chrismiceli at 7:45 PM:

    Thanks for the link. That is more support that the code is correct and the gcc compiler is issuing an invalid warning.

    Good stuff!

  13. #13
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Roaring_Tiger
    I believe your code looks correct to me as C does not care what you pass to function. So felt like some problem with worning level.
    What exactly do you mean by that?
    Thanks for the link. That is more support that the code is correct and the gcc compiler is issuing an invalid warning.
    still doubt that

    Why do I get this warning for the below simple example?
    "prog.c:9: warning: passing arg 1 of `digital_display' from incompatible pointer type"
    try passing it the argument type you gave in the prototype
    Code:
    void digital_display( const char digit[10][9] )
    {
    }
    
    int main ( void )
    {
      const char digit[10][9];
    
      digital_display( digit );     // this is line 9
     
      return 0;
    also
    Code:
    char digit[10][9]
    nothing really wrong with that. but making both subscripts symbolic constants would not only make it more readable, but easier to maintain, spot bugs and expand in case you want to make digit bigger.
    Code:
    #define ROW 10
    #define CLM 9
    
    const char digit[ROW][CLM];
    Last edited by caroundw5h; 09-06-2004 at 01:22 AM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    The array when passed to function would usually decay to char (*digit)[9], but you are attempting to convert it to const char (* digit)[9]. I believe, but am not certain, that the conversion from nonconst to const only works with respect to a single const type. Take, for example, "const char (* digit)[9]. The pointer to the array is not constant, each individual member of the array pointed to is constant. Hence, your're not just asking the compiler to convert a constant to non-constant; you're asking the compiler to convert many non-constants to many constants. I don't believe this is possible.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    oh...maybe I'm misunderstanding what const means. I thought it meant that the function would not modify the argument (treat it as a constant). In this case, that "digit" would not be modified by the function.

    Ok...so the correct code would be, as caroundw5h says, to add "const" in the main program.
    (or take "const" off the function prototype).

    Right?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. compiler warning message
    By alice in forum C Programming
    Replies: 2
    Last Post: 04-18-2004, 01:53 PM
  4. bcc32 compiler warning when assigning null to pointer
    By finnepower in forum C++ Programming
    Replies: 4
    Last Post: 06-25-2002, 12:37 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21