Members of a typedef struct

Discussion in 'C Programming' started by Diego Acevedo, Dec 28, 2005.

  1. How do I assess members of a typedef struct?


    Example:

    typedef struct {
    Int16 data1;
    Int32 data2;
    Char stringData[20];
    } MyRecordType;

    How would I assess the stringData member?

    Thanks,

    Diego Acevedo
    Diego Acevedo, Dec 28, 2005
    #1
    1. Advertising

  2. Diego Acevedo

    Artie Gold Guest

    Diego Acevedo wrote:
    > How do I assess members of a typedef struct?
    >
    >
    > Example:
    >
    > typedef struct {
    > Int16 data1;
    > Int32 data2;
    > Char stringData[20];
    > } MyRecordType;
    >
    > How would I assess the stringData member?
    >
    > Thanks,
    >
    > Diego Acevedo


    There is no such thing as a `typedef struct'. What you have done above
    is create a synonym for the anonymous struct you've defined.

    Of course, as there are no such types as `Int16', `Int32' and `Char' in
    standard C, I would "assess" the `stringData' member as meaningless.

    If you want to *access* a member, it's simple. If you have an instance
    of the struct, you use the `.' operator, if you have a pointer to an
    instance you use the `->' operator.

    But first, you read the FAQ and get a *good* book to use as a reference
    (see http://www.accu.org for suggestions).

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
    Artie Gold, Dec 28, 2005
    #2
    1. Advertising

  3. Diego Acevedo

    Guest

    There is no 'typedef struct', it is just an alias

    if you declare an Structure
    eg:
    structure a
    {
    short int data1;
    int data2;
    };

    to create an variable of 'structure a'

    the syntax would be
    struct a <userdefined variable name>

    insted if you use

    typedef structure a
    {
    short int data1;
    int data2;
    } A;

    to create an variable of 'structure a'

    the syntax would be
    A <userdefined variable name>

    and there is no Int16 and Int32;

    I believe these should be typedefed some where else as

    typedef Int16 short int
    typedef Int32 int
    typedef Char char

    there is no difference in accessing them

    For accessing

    Declare an variable

    MyRecordType myRec;

    Accessing data1 and data2

    myRec.data1 = 20;
    myRec.data2 = 40;

    Accessing stringData

    strcpy (myRec.stringData, "Text max 20 chars");

    Best Regards,
    Abdur
    , Dec 29, 2005
    #3
  4. Diego Acevedo

    haroon Guest

    Diego Acevedo wrote:
    > How do I assess members of a typedef struct?

    [...]

    > typedef struct {
    > Int16 data1;
    > Int32 data2;
    > Char stringData[20];
    > } MyRecordType;
    >
    > How would I assess the stringData member?


    assuming you have Int16... etc. typedef'ed, you can do it like this:
    /***/
    MyRecordType mrt, *pMrt;
    mrt.stringData;
    pMrt->stringData;
    /***/
    haroon, Dec 29, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DanielEKFA
    Replies:
    8
    Views:
    589
    DanielEKFA
    May 16, 2005
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,354
    Chris Fogelklou
    Apr 20, 2004
  3. Steven T. Hatton
    Replies:
    2
    Views:
    660
  4. oor
    Replies:
    0
    Views:
    1,334
  5. John Reye
    Replies:
    28
    Views:
    1,337
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page