wierd struct..never saw before..help!

This is a discussion on wierd struct..never saw before..help! within the C++ Programming forums, part of the General Programming Boards category; here is code Code: #include <windows.h> #include <iostream> using namespace std; struct { int iIndex; TCHAR* szLabel; TCHAR* szDesc; } ...

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    wierd struct..never saw before..help!

    here is code

    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    struct
    {
    	int iIndex;
    	TCHAR* szLabel;
    	TCHAR* szDesc;
    }
    
    sysmetrics [] = 
    {
    	SM_CXSCREEN,		TEXT("SM_CXSCREEN"),
    						TEXT("Screen width in pixels"),
    };
    
    int main()
    {
    	cout << sysmetrics[0].szLabel << endl;
    	return 0;
    }
    i always it had to be 'struct variableName' and you have to declare a struct in your main()....and how is szLabel set to SM_CXSCREEN? i dont get this wierd format please help
    nextus, the samurai warrior

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    This is just variable initialization (of an unnamed struct):
    Code:
    int n = 5;
    int a[] = {1,2,3,4,5};
    
    struct myStruct_t
    {
       int a,b;
    };
    
    myStruct_t ms = {1,2};
    
    //or
    
    struct yourStruct_t
    {
       char *str1, *str2;
    } ys = {"Hello","World"};
    
    //or
    
    struct /*I've got no type name!*/
    {
       int a;
       char *str1, *str2;
    } noName = {10,"str1","str2"};

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    C/C++ doesn't care if you put in some extra line breaks in your code. The way it's in your code is, IMO, a bad structure since it seems like sysmetrics is an array of strings or something instead of the structure.

    Also, why is it defined as an array?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i always it had to be 'struct variableName'
    C++ automatically typedef's the structure tag so that you can use it without the struct keyword:
    Code:
    struct tag
    {
      // Stuff
    };
    
    tag var1; // Legal C++, illegal C
    struct tag var2; // Legal C++ and legal C
    >and you have to declare a struct in your main()
    Where you declare the struct depends on style and how much scope you wish it to have in the program.

    >i dont get this wierd format
    Keep in mind that C++ is a free form language, if plenty of newlines and whitespace improve readability, you can do so. But I agree that the formatting could be better, what it does is define an anonymous structure and uses that as the type for an array called sysmetrics of size defined by an initialization list and then uses an initialization list to assign values to the array. Of course, since the initialization list only defines one item, there's no need for an array.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. wierd struct error
    By sujeet1 in forum C Programming
    Replies: 5
    Last Post: 09-27-2007, 06:38 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM

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