Thread: typedef struct vs struct declaration error in code

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    24

    typedef struct vs struct declaration error in code

    Hi All,

    I was wondering that why this snippet of code gives error
    error: conflicting types for 'fun'|
    Code:
    struct{
      u8 Address1;
      u8 Address2;
    }st_varib;
    
    
    
    
    
    
    struct st_varib gInstantReturn; 	/* global variable */
    
    
    struct st_varib fun(); 		/*Prototype of function fun*/
    
    
    struct st_varib fun()			/* fun function definition*/
    {
        printf("Hi");
    	gInstantReturn.Address1 = 5;    /*some action inside this function and final value would be save in some global variable or return*/
    	gInstantReturn.Address2 = 5;
        return gInstantReturn;
    }
    
    
    int main ()
    {
    	gInstantReturn = fun();
    	return 3;
    }
    while another one doesn't. I just add typedef keyword before the data type declaration


    Code:
    typedef struct{
      u8 Address1;
      u8 Address2;
    }st_varib;
    
    
    
    
    
    
    st_varib gInstantReturn; 	/* global variable */
    
    
    st_varib fun(); 		/*Prototype of function fun*/
    
    
    st_varib fun()			/* fun function definition*/
    {
        printf("Hi");
    	gInstantReturn.Address1 = 5;    /*some action inside this function and final value would be save in some global variable or return*/
    	gInstantReturn.Address2 = 5;
        return gInstantReturn;
    }
    
    
    int main ()
    {
    	gInstantReturn = fun();
    	return 3;
    }
    Any idea would be appreciated.


    Thanks

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,731
    Because the first creates an unnamed struct and then declares a variable named "st_varib", while the second creates an unnamed struct and then gives it a new type named "st_varib".

    TL;DR:
    In the first one, "st_varib" is a variable.
    In the second one, "st_varib" is a type.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by shaswat View Post
    Hi All,

    I was wondering that why this snippet of code gives error

    Code:
    struct{
      u8 Address1;
      u8 Address2;
    }st_varib;
    ...
    If you're not using typedef, you probably want to be using this form instead, judging from the rest of your code:

    Code:
    struct st_varib {
        u8 Address1;
        u8 Address2;
    };
    This declares struct st_varib as a type. Remember that struct tags have a separate name space, so the following is a valid C statement which declares a symbol called st_varib whose type is struct st_varib:

    Code:
    struct st_varib st_varib;
    Some programmers find that confusing, so they declare a typedef like this:

    Code:
    struct st_varib {
        u8 Address1;
        u8 Address2;
    };
    typedef struct st_varib st_varib;
    After writing that, you can now say 'st_varib s;' to declare that s has type 'st_varib' (which is the same as type 'struct st_varib').

    Some programmers find the above confusing because 'st_varib' appears twice with different meaning, so they write this equivalent definition instead:

    Code:
    struct _st_varib {
        u8 Address1;
        u8 Address2;
    };
    typedef struct _st_varib st_varib;
    This version creates a throwaway symbol '_st_varib' in the struct tag name space which the programmer intends to use exactly one time (in the typedef line). From then on he uses 'st_varib' as the type name. Finally the above can be simplified to the following (which you used in your example):

    Code:
    typedef struct {
        u8 Address1;
        u8 Address2;
    } st_varib;
    This version declares the struct as an anonymous struct but then typedef's it to the name st_varib. So again you can write 'st_varib s;' to declare a symbol called s which has the type 'st_varib' which is now the same as the struct type. It is conceptually equivalent to writing

    Code:
    struct { u8 Address1; u8 Address2; } s;
    except that writing the above will probably not work very well, because each time you write struct with a member definitions list, it introduces a new type, even if the struct member definitions are exactly the same as a previous definition.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    Thanks for elaborating and such a great answer. Your answer leads me to understand more about typedef's.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct declaration failed. (very few lines of code)
    By tmac619619 in forum C Programming
    Replies: 3
    Last Post: 09-14-2015, 03:35 PM
  2. differences between struct and typedef struct
    By stanlvw in forum C Programming
    Replies: 1
    Last Post: 07-22-2008, 03:28 PM
  3. syntax error: enum and typedef struct
    By sagitt13 in forum C Programming
    Replies: 2
    Last Post: 10-31-2006, 04:17 PM
  4. Diff btw typedef struct and struct?
    By willkoh in forum C Programming
    Replies: 3
    Last Post: 09-24-2005, 02:27 PM
  5. typedef struct vs. struct
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-26-2002, 12:47 PM

Tags for this Thread