structure variables

This is a discussion on structure variables within the C++ Programming forums, part of the General Programming Boards category; Hi A structure defines a collection of related data (Data could be in different forms, e.g. variables and constants are ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    structure variables

    Hi

    A structure defines a collection of related data (Data could be in different forms, e.g. variables and constants are two types of data. Am I correct?) about something as a new type (I think using "type" is not a good choice here because in itself a structure has no 'type', what do you say?) with an identifier. In other words, a structure, let's say St1, combines two or more kinds of variables under the same umbrella, and then whenever some other variable, x, is declared using St1 x; one needs to assign as much values to x as there are in the St1. A structure is an 'umbrella' variable declaration. Please correct me.

    These points should be remembered:
    1: The variable, such as x above, declared using a structure cannot be read or written like a normal variable such as int y. e.g. You cannot write: cin >> x, or, cout << x.
    2: One structure variable could be assigned to another variable of the same structure. e.g. If x and z both are St1 variables, then it can be written: x = y.
    3: The comparison of structure variables cannot be carried out using relational operators like regular variables. e.g. One cannot do: x > z.
    4: A structure can be nested within another structure.

    Please review the above material and please make corrections if necessary. Thanks a lot.

    And if you run or check out the output of the below given code you will notice that the second tab character "\t" produces more space than the first. Notice the distance between "1" and "2", and between "2" and "3".

    Sample code:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    struct date
    {
        int day; int month; int year;
    };
    
    int main ()
    
    {
        date D1, D2;
    
        {
            cout << "Enter the data for Day No. 1" << endl;
            cout << "Enter Day: "; cin >> D1.day;
            cout << "Enter Month: "; cin >> D1.month;
            cout << "Enter Year: "; cin >> D1.year;
        }
    
            cout << " " << endl;
    
        {
            cout << "Enter the data for Day No. 2" << endl;
            cout << "Enter Day: "; cin >> D2.day;
            cout << "Enter Month: "; cin >> D2.month;
            cout << "Enter Year: "; cin >> D2.year;
        }
    
        cout << " " << endl;
    
        cout << "Day No. 1 Detail: " << D1.day << "\t" << D1.month << "\t" << D1.year << endl;
    
        cout << "Day No. 2 Detail: " << D2.day << "\t" << D2.month << "\t" << D2.year << endl;
    
        system("pause");
    
    }
    Output:
    Code:
    Enter the data for Day No. 1
    Enter Day: 1
    Enter Month: 2
    Enter Year: 3
    
    Enter the data for Day No. 2
    Enter Day: 4
    Enter Month: 5
    Enter Year: 6
    
    Day No. 1 Detail: 1     2       3
    Day No. 2 Detail: 4     5       6
    Press any key to continue . . .
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > 3: The comparison of structure variables cannot be carried out using relational operators like regular variables. e.g. One cannot do: x > z.
    They can in C++, if you provide an implementation for the appropriate operators.

    Also, in C++, this class is the same as a struct
    Code:
    class foo {
      public:
    };
    And this struct is the same as a class
    Code:
    struct foo {
      private:
    };
    The only difference is the default visibility of members.

    structs, classes, unions and arrays are collectively called aggregate types
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Quote Originally Posted by jackson6612 View Post
    A structure defines a collection of related data (Data could be in different forms, e.g. variables and constants are two types of data. Am I correct?) about something as a new type (I think using "type" is not a good choice here because in itself a structure has no 'type', what do you say?) with an identifier. In other words, a structure, let's say St1, combines two or more kinds of variables under the same umbrella, and then whenever some other variable, x, is declared using St1 x; one needs to assign as much values to x as there are in the St1. A structure is an 'umbrella' variable declaration. Please correct me.
    A struct creates a new type, because you can only create new variables by giving them a type.

    1: The variable, such as x above, declared using a structure cannot be read or written like a normal variable such as int y. e.g. You cannot write: cin >> x, or, cout << x.
    3: The comparison of structure variables cannot be carried out using relational operators like regular variables. e.g. One cannot do: x > z.
    Incorrect. You can do this provided you tell the compiler how to do it. The compiler knows nothing of your structure, so it cannot know how to perform those operations.
    But you can tell the compiler how to do it by overloading operators. Then it works fine.

    2: One structure variable could be assigned to another variable of the same structure. e.g. If x and z both are St1 variables, then it can be written: x = y.
    4: A structure can be nested within another structure.
    Correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, Elysia. You are so nice.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-15-2010, 07:12 AM
  2. structure variables
    By sarathius in forum C Programming
    Replies: 8
    Last Post: 04-15-2008, 01:49 AM
  3. initializing structure variables
    By cs32 in forum C Programming
    Replies: 2
    Last Post: 04-11-2008, 05:33 PM
  4. passing structure variables into function
    By sarathius in forum C Programming
    Replies: 1
    Last Post: 04-07-2008, 11:56 PM
  5. referencing structure variables
    By sballew in forum C Programming
    Replies: 1
    Last Post: 11-01-2001, 12:56 PM

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