Thread: <anonymous struct> sucks

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74

    Unhappy <anonymous struct> sucks

    Code:
    int main()
    {
        struct
        {
            int m_aa;
            int m_bb;
        } a = {0, 1};
    
        struct
        {
            int m_aa;
            int m_bb;
        } b;
    
        b = a; // sucks here
        return 0;
    }
    I think a and b is of the same type and there is no error with the assignment. But the compiler give me an error: incompatible types when assigning to type ‘struct <anonymous>’ from type ‘struct <anonymous>’.

    So how can I make "b = a" legal? (I know memcpy() can do that but that's not what I want. I want to implement std:air<> in C using macros.)

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    They are different types in C. Even if type,name of fields are the same.
    Now the question is why would one want to do this.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    Quote Originally Posted by Bayint Naung View Post
    They are different types in C. Even if type,name of fields are the same.
    Now the question is why would one want to do this.
    That may be a little crazy, but I want to implement syntax-candy like this:
    Code:
    MAPITEM_TYPE(int, const char*) mapitem;
    
    MAPITEM_KEY(mapitem) = 1;
    MAPITEM_VAL(mapitem) = "Hello";
    AVLTREE_INSERT(tree, mapitem);
    
    MAPITEM_KEY(mapitem) = 2;
    MAPITEM_VAL(mapitem) = "World";
    AVLTREE_INSERT(tree, mapitem);

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RichSelian View Post
    Code:
    int main()
    {
        struct
        {
            int m_aa;
            int m_bb;
        } a = {0, 1};
    
        struct
        {
            int m_aa;
            int m_bb;
        } b;
    
        b = a; // sucks here
        return 0;
    }
    I think a and b is of the same type and there is no error with the assignment. But the compiler give me an error: incompatible types when assigning to type ‘struct <anonymous>’ from type ‘struct <anonymous>’.

    So how can I make "b = a" legal? (I know memcpy() can do that but that's not what I want. I want to implement std:air<> in C using macros.)
    try this....
    Code:
    struct tNums
       {
            int m_aa;
            int m_bb;
        };
    
    struct tNums a, b;
    Now you have two of the same type... and in less code to boot.
    Last edited by CommonTater; 04-04-2011 at 11:04 AM.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    Quote Originally Posted by CommonTater View Post
    START YOUR OWN THREAD ... Do not hijack someone else's!

    Gees!



    try this....
    Code:
    struct tNums
       {
            int m_aa;
            int m_bb;
        }
    
    struct tNums a, b;
    Now you have two of the same type... and in less code to boot.
    Of course I know that. But I'm trying generic programming in C so I need macros to do that.
    For example, now I have an implementation of avltree and want to use it as a dictionary. I have to write a structure first, and define cmp() function for the structure type. That's not convenient. I just want to find an easier way to do that.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You're asking for ugliness!

    Code:
    #define PAIR_INIT(T1,T2)   \
     typedef struct {   \
          T1   first;             \
          T2 second;         \
    } pair##T1##T2
    
    #define PAIR(T1,T2)                     pair##T1##T2
    #define make_pair(T1,T2,v1,v2)    (pair##T1##T2) { .first = v1, .second = v2 }
    typedef char * String;
    PAIR_INIT(String,double);
    
    
    int main(void)
    {
    	PAIR(String,double) p;
    p = make_pair(String,double,"foo",3.0);
    
    printf("%s %f\n", p.first,p.second);
    return 0;
    }
    Note: you need to typedef char* String;
    C does not have generic.
    I still don't see how generic solves your problem?
    Perhaps you should follow C approach. Take a look at bsearch(),qsort(). They are examples of doing generic in C way.
    Last edited by Bayint Naung; 04-04-2011 at 11:06 AM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Also, you can look at the GLIB source. They actually implement a balanced binary tree that takes generic data types, and uses separate key and data elements. Start here: GTK+ - Download.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If you use a typedef template and consistently use only that name for the actual definitions I'm sure it will work. The compiler then has no excuse to think different occurrences of the struct are different in any way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. this REALLY sucks
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-31-2004, 12:46 AM
  2. How Cool is Java
    By dukemarlon in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 11-28-2002, 05:24 PM
  3. Why europe sucks
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 04-08-2002, 07:02 PM
  4. Why the DX8.1 SDK sucks....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-06-2002, 12:07 AM
  5. Microsoft Sucks
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 110
    Last Post: 11-08-2001, 04:30 PM