This blog is under construction

Sunday, 13 May 2012

What is a structure?

A structure is a user defined compound data type.  It is also defined as the elements of different data types grouped under a common name.  Below is the general form of a structure.

struct  <name> {
      data_type   <data_element1>;
      -------------
};

Consider the following example,
struct value {
          int x;
          float y;
};

Above is the definition for a structure named value and it contains an integer variable x and float variable y as its structure member.

Structure example in C:

  #include <stdio.h> 
  struct student {
        char name[100];
        int rollno;
        int age;
  };

  int main() {
        /* s1 is a variable of type struct student */
        struct student s1 = {"jp", 1010101, 23};  // assigning values to variable s1
        printf("Name: %s\n", s1.name);
        printf("Roll No.:%d\n", s1.rollno);
        printf("Age: %d\n", s1.age);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Name: jp
  Roll No.:1010101
  Age: 23



No comments:

Post a Comment