unions and structs

This is a discussion on unions and structs within the C Programming forums, part of the General Programming Boards category; I can't understand why I get the following errors. Here is my code: Code: union header{ /* block header [LINE ...

  1. #1
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151

    unions and structs

    I can't understand why I get the following errors.
    Here is my code:

    Code:
    union header{ /* block header         [LINE 11] */
    	struct{
    		unsigned int size; /* size of this block */
    		int prev_check = 0;
    		union header *ptr; /* next block if on free list */
    
    	} s;
    	Align x; /* force alignment of blocks */
    };
    
    typedef union header Header; /*[LINE 20]*/
    These are the errors that I get:
    Code:
    mymalloc.c:14: warning: no semicolon at end of struct or union
    mymalloc.c:14: error: syntax error before '=' token
    mymalloc.c:14: warning: no semicolon at end of struct or union
    mymalloc.c:17: error: syntax error before '}' token
    mymalloc.c:17: warning: data definition has no type or storage class
    mymalloc.c:19: error: syntax error before '}' token
    I am getting this error just by declaring a variable: int prev_check. I wont get these errors when I remove that line. Please advise.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't initialize variables in a union declaration. In other words: no = 0 allowed.

  3. #3
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Just took a baton and hit my head.

  4. #4
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    What should I be searching for if I get the following:

    Code:
    warning: implicit declaration of function `morecore'
    warning: assignment makes pointer from integer without a cast
    At top level:
    error: conflicting types for 'morecore'
    error: previous implicit declaration of 'morecore' was here
    This is the snippet of code
    Code:
    if (p == freeptr) /*p & freeptr are pointers */
         if ((p = morecore(nunits)) == NULL)  /*morecore returns a pointer */
    	 return NULL; /* none left */

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You might be missing a header file -- apparently there was no declaration of morecore in scope at the first part. (The second one is probably your definition of morecore, with the proper types, which don't match the implicit int.)

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    The implicit declaration warning (and the assignment one) means that the function isn't declared at the point where you call it; you probably forgot to put a prototype in or include a header file.

    On the other hand, the other errors mean that your prototype and function declaration don't match.

    At a guess, you probably changed morecore() somehow and forgot to update the prototype, and the prototype itself occurs after the code you posted or in a different file.

    Perhaps you have two functions called morecore as well, but that's somewhat less likely . . . .

    [edit] Here I was assuming that you wrote morecore() yourself. tabstop's idea is probably more likely. [/edit]
    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.

  7. #7
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Well you were right, I forgot to add it in the header file. But after adding it (same function statement) I get 2 error for previous declaration and conflicting type errors for the same line in the header file as well as c file.
    Code:
    mymalloc.c:69: error: conflicting types for 'morecore'
    mymalloc.h:6: error: previous declaration of 'morecore' was here
    mymalloc.c:69: error: conflicting types for 'morecore'
    mymalloc.h:6: error: previous declaration of 'morecore' was here

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So now there's a conflict between your prototype in your header file and reality (as expressed in your c file). Just copy the top line of your function, change the curly-brace to a semicolon, and make that your prototype -- it will save heartache later on.

  9. #9
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    As I had said in my earlier mail, I had done so. If you look at the earlier errors mentioned in this forum. Im thinking does it have anything to do with the warning : assignment makes pointer from integer without a cast.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ron View Post
    As I had said in my earlier mail, I had done so. If you look at the earlier errors mentioned in this forum. Im thinking does it have anything to do with the warning : assignment makes pointer from integer without a cast.
    You just said you aren't getting that warning any more.

    But no, what I mean is that your prototype might be something like
    Code:
    int * morecore(int something);
    but your code actually has
    Code:
    int morecore(int something) {
      /* stuff */
    }

  11. #11
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    thank you 'tabstop', but that is what I had done. Still the errors.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ron View Post
    thank you 'tabstop', but that is what I had done. Still the errors.
    What I'm trying to get you to do is show us line 69 of mymalloc.c and line 6 of mymalloc.h, which are the two lines mentioned in the last error message you posted.

  13. #13
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Code:
    line 6
    static Header *morecore(unsigned long nunits);
    
    line 69
    static Header *morecore(unsigned long nunits)
    {
    
    
    
    }

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, I've tried to recreate the error, but can't. (I had at first thought that a forward declaration of Header might cause problems, but that seemed to work just fine.) The only thing I can think of at this point is that Header might mean different things at different times? That's the best guess I can make right now.

  15. #15
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Did you use the function before its declaration?

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

Similar Threads

  1. Unions & Structs... Puzzled.
    By Sparrowhawk in forum C Programming
    Replies: 10
    Last Post: 12-14-2008, 03:45 PM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  3. Possible to create methods for data in structs?
    By eccles in forum C Programming
    Replies: 19
    Last Post: 12-15-2004, 08:46 AM
  4. why unions and structs couldnt be initialized ?
    By black in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2002, 04:05 AM
  5. Structs and Unions
    By Encrypted in forum C Programming
    Replies: 4
    Last Post: 11-05-2002, 02:53 AM

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