Twitter Updates

    follow me on Twitter

    List for 4.5% and get 1% cash back on your purchase

    Monday, April 20, 2020

    Structure Part I: The Basics

     

    Today we are going to go through Structures from defining structures to using structures.
    Structures are just a collection of different types under one roof (you can even put one type only!). So that means they give you flexibility of grouping different data types (like int, char, or even char[]) under one name.
    So let us start with obviously defining a Structure. In `C` we declare a structure as simply as this:-
    struct dob {
        int day;
        int month;
        int year;
    };
    1: In the above code segment struct is a keyword which defines structure.
    2: Followed by struct keyword (dob) is the name of our structure.
    3: Elements of struct are defined inside braces '{}' as we did (int day; etc).
    4: After ending brace we place a terminator ';' to end the declaration.

    So now you know how to define a structure but how to create its instances now?
    To create a variable of our structure we just need to do this:
    struct dob date;
    This now declares date as a structure variable of type dob.
    1: Here 'struct dob' is our above declared structure.
    2: date is a variable of type dob.

    So ok we have a structure and a variable of that type but how can i access its parts?
    well we can access it and assign it so simply like this:-
    date.day = 19;date.month = 10;date.year  = 1990;
    Note here we use the dot (.) operator to access the fields (parts) of our structure.
    ok everything looks nice so for but how in the world can i read data into this structure variable? Again no worries its again simple:-
    scanf("%d", &date.day);scanf("%d", &date.month);
    that was pretty easy but I was wondering how can i print its data?
    Just do it like this:-
    printf("Day: %d", date.day);printf("Month: %d",date.month);printf("Year: %d", date.year);
    Again remember we use dot (.) operator to access members of a structure.
    So we now know how to define and declare a structure, how to access its members, how to read data in it, and how to print data of a structure. Oh that was a tough job..!
    Now let us put it together in a single C Program.
    /***********************************************/
    #include <stdio.h>
    struct dob {
       int day;
       int month;
       int year;
    };

    int main(void) {
      struct dob date;
      date.day = 19;
      date.month = 10;
      date.year = 1990;

      printf("Day is : %d, Month is: %d, and Year is %d\n",
               date.day,date.month, date.year);

      printf("Enter Day, Month, and Year separated by spaces: ");
      scanf("%d %d %d", &date.day,&date.month,&date.year);
      printf("Your entered Date is: %d/%d/%d",
      date.day,date.month,date.year);

      return 0;
    }
    Output:
    Day is : 19, Month is: 10, and Year is 1990
    Enter Day, Month, and Year separated by spaces: 1 1 2014
    Your entered Date is: 1/1/2014

    Continue reading


    No comments:

    Post a Comment

    Home for sale- $2,000 rebate!

    Ready Real Estate slide show

    Become a fan of my page

    Sheree Dutton, Reatlor, DFW, Texas on Facebook
    Powered By Blogger

    Pandora Faves

    Back on the market, price reduced, 1% cash back rebate offered

    Sheree Dutton | Ready Real Estate | 817-975-0461
    222 Birchwood, Azle, TX
    Back on the market, price reduced and 15 cash back rebate offered!
    3BR/2BA Single Family House
    offered at $102,500
    Year Built 2006
    Sq Footage 1,142
    Bedrooms 3
    Bathrooms 2 full, 0 partial
    Floors 1
    Parking 3 Covered spaces
    Lot Size .225 acres
    HOA/Maint $0 per month

    DESCRIPTION


    Wow, talk about pride of ownership! This house has too many upgrades to count, and is so well cared for. You must see it to believe it! A lot of value in this perfect starter home.

    OPEN HOUSE SUNDAY MAY 3RD 2+5 pm

    see additional photos below
    PROPERTY FEATURES

    - Central A/C - Central heat - Fireplace
    - High/Vaulted ceiling - Walk-in closet - Tile floor
    - Living room - Breakfast nook - Dishwasher
    - Refrigerator - Stove/Oven - Microwave
    - Laundry area - inside - Balcony, Deck, or Patio - Yard

    OTHER SPECIAL FEATURES

    - 1 car garage, covered carport for 2 cars
    - covered wood deck in backyard
    - gutters
    - storage shed
    - newly stained wood fence
    - electric fireplace added, with tile hearth
    - upgraded ceiling fans and light fixtures
    - island in kitchen

    ADDITIONAL PHOTOS


    Fantastic curb appeal

    covered wood deck in back

    living room

    kitchen with island

    breakfast nook

    master bedroom
    Contact info:
    Sheree Dutton
    Ready Real Estate
    817-975-0461
    For sale by agent/broker

    powered by postlets Equal Opportunity Housing
    Posted: Sep 11, 2009, 7:31am PDT

    Blog Archive