Thread: typedef again!

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Question typedef again!

    struct hello
    {
    char d;
    }instance;

    Accession to the structure field: instance.d = 'A'

    AND

    struct
    {
    char d;
    }hello;

    Accession to the structure field: ??

    AND

    typedef struct
    {
    char d;
    }HELLO;
    HELLO hello;

    Accession to the structure field: hello.d = 'A'

    AND

    typedef struct hello
    {
    char d;
    }HELLO;

    Accession to the structure field: ??.

    How do I diffentiate them? They look almost the same!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    struct hello 
    { 
      char d; 
    } instance;
    >Accession to the structure field: instance.d = 'A'
    Correct.
    Code:
    struct 
    { 
      char d; 
    } hello;
    >Accession to the structure field: ??
    hello.d = 'A';
    Code:
    typedef struct 
    { 
      char d; 
    } HELLO;
    
    HELLO hello;
    >Accession to the structure field: hello.d = 'A'
    Correct.
    Code:
    typedef struct hello 
    { 
      char d; 
    } HELLO;
    >Accession to the structure field: ??.
    First you have to create an instance.

    struct hello inst1;
    HELLO inst2;

    inst1.d = 'A';
    inst2.d = 'B';

    Both will work with the same effect.

    >They look almost the same!
    They all have very slight differences though. The first is a struct declaration with a tag and an instance. So while it has at least one instance, more can be made with the tag. The second has no tag and an instance, that instance is the only one you can have because there's no tag for further declarations. The third uses typedef to create a type for an unnamed struct, so you can use the type in declarations even though there is no tag. The fourth has both a tag and a type, so either method of declaration can be used: with the struct keyword and the tag, or just the type.

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

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Question errr

    >>The first is a struct declaration with a tag and an instance. So while it has at least one instance, more can be made with the tag. The second has no tag and an instance, that instance is the only one you can have because there's no tag for further declarations.

    If the above statement is true, then, why the second struct (which has no tag) is still used since it can be replaced by the first struct declaration? All I need to do is just declare one instance and it is exactly same as the second struct which has also one instance only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM