Thread: "differs in levels of indirection" warning

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    "differs in levels of indirection" warning

    Hi there,

    My compiler gave me the following warning for some code:

    \main.c(83) : warning C4047: 'initializing' : 'float *' differs in levels of indirection from 'int'

    line 83 reads:

    Code:
    float *weights = malloc(n* sizeof *weights);
    and is a pointer declaration that is inside a function other than main (local to?, still getting used to the meta-language). The code seems to work fine but I am interested in what the compiler is trying to tell me here.

    Any comments would of course be greatly appreciated.

    Boggle.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    When the compiler comes across a call to a function that it does not have a prototype for, it assumes that the compiler will return int. Since you have not included <stdlib.h> and thus do not have the prototype for malloc(), it's like you'd gone
    Code:
    int x;
    float *f = x;
    which of course the compiler thinks is suspect.

    Just include <stdlib.h> and the warning should go away.

    (It's why casting malloc() is not recommended, BTW. If you cast malloc(), it hides this programming error.)

    The FAQ has an article about this. http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Cheers for the reply - I think I get it now. I had read the FAQ you suggest but am a newbie so the subtlety of this point didn't sink in. Thanks again.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, if you enable "compiler warnings" when you build your project, it's highly likely that the compiler will tell you something like "malloc hasn't got a prototype, int return assumed".

    Compiler warnings are created to help the programmer - I actually "like" having the flag to say "treat warnings as errors" enabled - that way, you won't even get something that runs when the compiler gives warnings [I do realize that sometimes it is not possible to avoid warnings: I had a piece of code that containes a switch statement, which the compiler complained didn't cover all the possible cases [which in my mind I had covered, as the switch input was an enum and I'd done all the enum values] (and no default) - when I added a "default: return -1;" it says "Code can't be reached" and I said "Make up your mind, stupid compiler"... No way to resolve that one completely....]

    --
    Mats

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One way to resolve that would be to take what you think would make a good default or a common case, or just the last enum value, and change it from "case WHATEVER:" to "default:".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM