This is a discussion on structure within the C Programming forums, part of the General Programming Boards category; hello guys, can we use same name for both structure name and it's variable. thak u bye srinu...

  1. #1
    Registered User
    Join Date
    May 2002

    Talking structure

    hello guys,

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

    thak u

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

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

  3. #3
    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
    struct meaninglesslabel
      int var;
    I think this is better, it's been many years since I programmed.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >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:
    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;
    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

    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, 06: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, 07: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

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