Structure

This is a discussion on Structure within the C Programming forums, part of the General Programming Boards category; Can anyone please explain what var1 is in the following? A sructure i.e. struct ABC { int x; int y; ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Question Structure

    Can anyone please explain what var1 is in the following?

    A sructure i.e. struct ABC
    {
    int x;
    int y;
    } ABC_1;

    Variable declared as: static ABC_1 var1;

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up Its just a variable

    Hi there,
    let me try to explain what var1 is...

    When You Say

    int i;

    you inform the compiler to reserve memory (as much as needed for an integer which can be two or four bytes depending on the environment you are using) that can hold an integer value and assign it the name 'i'.

    Now, you have declared a Structure (user defined data type). So, you want to store three different data-items in three different variables but want them to be bound by a common name. So you have created a data type. Next step requires that you declare a variable of that data type (just like int i). Here is the problem, since the C compiler does not recognise the user-defined datatypes as standard, you inform this by saying...
    struct ABC <<variable_name>>;

    This reserves the memory required by the variables of the structure ABC and assigns the common name var1 to them. So, you can then refer to individual elements using the '.' notation like...
    var1.x
    var1.y
    etc...
    Hope you catch the point,,,

    Regards,

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Wait Wait....
    Im really sorry I did not go through ur question properly...
    Letme check, and I'll get back to u again...
    Sorry again.

    Regards,
    Help everyone you can

  4. #4
    Sayeh
    Guest
    Well, you are declaring a variable of type "ABC_1", and it is persistent only to the scope of the file it's in.

    Code:
    typedef struct                           /* create new type - the correct way */
       { 
       int x; 
       int y; 
       }ABC_1; 
    
    static ABC_1 var1;                    /* declare a variable of the new type */
    
    int foo(*ABC_1);                       /* prototypes */
    int main(void);
    
    /* funcs */
    
    int main(void)
       {
       int equality = 0;                     /* init vars */
    
       var1.x = 5;                             /* init fields in struct */
       var1.y = 5;
    
       equality = foo(&var1);           /* pass struct by reference */
    
       return(0);
       }
    
    int foo(*ABC_1 v)
       {
       if(v->x == v->y)                     /* compare fields in struct */
          return(1);
       else
          return(0);
       }
    this shows it to you and how it might be used.

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