Thread: What is a structure tag?

  1. #16
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It's not valid ISO C++...

  2. #17
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Also, why would you want to bother with code that breaks so easily by simply changing compilers/platforms or even adding or reordering members?
    O_o

    Why did you write similar code to benefit from the C++ mechanisms while also benefiting from SSE when available?

    Actually, that code has a practical purpose. Let us look at a purely cosmetic situation; why do you write code having member variables of the form `_E???' and `_M???'?

    I'm not even picking on you; I just find question rather bizarre from someone who also writes code in clear violation of the standard for no better reason than the look of the thing.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #18
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It's not valid ISO C++...
    o_O

    Wow.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by phantomotap View Post
    Why did you write similar code to benefit from the C++ mechanisms while also benefiting from SSE when available?
    Dunno what you're referring to here...

    Actually, that code has a practical purpose. Let us look at a purely cosmetic situation; why do you write code having member variables of the form `_E???' and `_M???'?

    I'm not even picking on you; I just find question rather bizarre from someone who also writes code in clear violation of the standard for no better reason than the look of the thing.
    Just because I do X doesn't mean I think it's a good idea that others do X.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Speaking of structs, I saw this example in a book a few weeks ago. I couldn't remember what it's called when you do this.

    You have a struct that is declared like this:

    Code:
    typedef struct tagBITMAPCOREINFO
    {
       BITMAPCOREHEADER bmciHeader; // Core-header structure
       RGBTRIPLE bmciColors[1]; // Color table
    
    } BITMAPCOREINFO, *PBITMAPCOREINFO;
    When you need to store 256 colors in the color table, you allocate like this (it's in C)

    Code:
       PBITMAPCOREINFO pbmci = malloc( sizeof( BITMAPCOREINFO ) + 255 * sizeof( RGBTRIPLE ) );
    Then you can access 256 indexes of the color table.

    Code:
       pbmci->bmciColors[200] = { 0xC0, 0xC0, 0xC0 };
    This seemed a bit weird to me. Aren't I actually accessing outside of the bounds of the array, and into the extra memory allocated for the structure? Is this ok (and does this concept have a name)?
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is a hack to simulate a flexible array member. I believe it is known as the "struct hack".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    To remove that pedantic error, you need to modify that internal struct:
    Code:
    struct point_t
    {
      union
      {
        struct
        {
          float x, y, z;
        } point;
        float p[3];
      };
    };
    Now you can assign to them (x, y, z) by the point identifier.
    Code:
    point_t pt;
    pt.point.x = 1.1;
    pt.point.y = 1.2;
    pt.point.z = 1.3;
    Just to be clear, so I can understand this as well:
    Code:
    #include <iostream>
    
    struct point_t
    {
      point_t() : point_t(0, 0, 0) {}
      point_t(float x, float y, float z)
      {
        static_assert(sizeof(point) == sizeof(float) * 3, "structure has padding");
        point.x = x;
        point.y = y;
        point.z = z;
      }
    
      union
      {
        struct
        {
          float x, y, z;
        } point;
        float p[3];
      };
    };
    
    int main()
    {
      using namespace std;
      point_t pt(1.3, 1.2, 5.7);
      cout << pt.point.x << ", " << pt.point.y << ", " << pt.point.z << endl;
      cout << pt.p[0] << ", " << pt.p[1] << ", " << pt.p[2] << endl;
    }
    Is this "acceptable"?

  8. #23
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I haven't personally tested it but thank you for fixing the pedantic error!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 03-18-2014, 10:43 AM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  4. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  5. Replies: 4
    Last Post: 11-22-2006, 12:20 PM