error: invalid initializer

This is a discussion on error: invalid initializer within the C Programming forums, part of the General Programming Boards category; 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( ...

  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,246
    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
    Katy, Texas
    Posts
    2,309
    Perhaps it's your tag name and variable name both being "foo" that is confusing malloc?

    edit - or what bithub said!!!!!!!!!!!!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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,246
    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,045
    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, 03:35 AM
  2. error: braces around scalar initializer...
    By Osiris990 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2008, 02:22 PM
  3. initializer element is not constant...
    By John Connor in forum C Programming
    Replies: 12
    Last Post: 02-01-2008, 05: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, 11:31 AM

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