Thread: structure

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    17

    Talking structure

    hello guys,

    can we use same name for both structure name and it's variable.

    thak u
    bye
    srinu
    srinu

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Yes, you can.

    Code:
    struct data
    {
          int data;
    }data;
    
    struct
    {
          int data;
    }data;
    Last edited by shaik786; 06-24-2002 at 05:23 AM.

  3. #3
    TK
    Guest
    Code:
    struct Data_t
    {
      int var;
    };
    struct Data_t Data;
    ...

    That's how I would do it, rather than make the code confusing. Apparently the worst thing that you have to watch out for is defining a local variable within a function with the same name as a global variable (that might be in a #include file). That will get you into trouble. The use of '_t' stands for 'type'.

  4. #4
    TK
    Guest
    Code:
    struct meaninglesslabel
    {
      int var;
    }Data_t;
    I think this is better, it's been many years since I programmed.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can we use same name for both structure name and it's variable.
    Sure, they're in different namespaces so you can do something like this:
    Code:
    typedef struct foo
    {
      int foo;
    } foo;
    From there you can only access the member foo by using the . or -> operators. Because of the typedef, you can also omit the required struct keyword when you create an instance of foo:

    struct foo var;
    or
    foo var;

    Will both create an instance of foo. This is because struct foo is the tag for a struct and thanks to typedef, foo is a new type, shorthand for struct foo { int foo; }. If you find that confusing, imagine this feature being used in real code. :P

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM