Thread: error: invalid initializer

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    error: invalid initializer

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct foo
    {
        unsigned int a:16;
        unsigned int b:8;
        unsigned int c:4;
    } foo;
    
    int main( void )
    {
         foo duh = malloc(sizeof(foo));
    struct foo *fe = duh;
        return 0;
    }
    I keep getting this error error: invalid initializer for malloc duh
    I also get error: incompatible types in initialization for the pointer pointing to duh Y are these errors
    Last edited by kiros88; 09-17-2009 at 03:11 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    malloc() returns a pointer. You are assigning a pointer value to something that is not a pointer.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Perhaps it's your tag name and variable name both being "foo" that is confusing malloc?

    edit - or what bithub said!!!!!!!!!!!!
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Okay so i got rid of Malloc for the first error to go away
    but I still cant get the pointer to point to the struct ? how is that suppose to look

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    struct foo *fe = &duh;
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For what it's worth, if you want bit fields with common sizes like 8 or 16, you can just use unsigned char and unsigned short. You can even use typedefs if you like.
    Code:
    typedef unsigned short uint_16;
    [That particular name might be defined elsewhere; it is in C, not sure about C++.]
    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: 12
    Last Post: 01-17-2009, 04:35 AM
  2. error: braces around scalar initializer...
    By Osiris990 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2008, 03:22 PM
  3. initializer element is not constant...
    By John Connor in forum C Programming
    Replies: 12
    Last Post: 02-01-2008, 06:28 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. initializer?
    By Luigi in forum C++ Programming
    Replies: 0
    Last Post: 11-29-2002, 12:31 PM