-->

Facebook

Jasper Roberts - Blog

Monday, April 20, 2015

Celebrating 10000 Visits.....Hurrey Hurrey!!!!



Friday, April 17, 2015

Chapter - 7 Structure

Structure                                                        |CLICK HERE
Accessing the structure members                |CLICK HERE
Array of Structure                                           |CLICK HERE
Difference between Structure and Union       |CLICK HERE

Wednesday, April 15, 2015

Pointers and Structures

For representing complex data structures, we can use structures and pointers together.

Pointer can be a member of structure, or a pointer to a structure, or an array of pointers to structure.

We can declare a pointer to structure as we do for pointers to basic data types.

Structure Syntax:
            struct structure_name
            {
                        datatype-1 variable-1;
                        datatype-2 variable-2;
                                    .
                                    .
                        datatype-n variable-n;
            }struct_variable, ponter-to-struct_variable;

Here, struct is a keyword which is used to define a structure template.
struct name is a name of the structure. It is user defined name.
The members of the structure are written in curly { } braces.
The structure is terminated by semicolon (;).

Structure to Pointer Example:
            struct student
            {
                        int rollno;
                        char name[50];
                        char branch[20];
                        float per;
            }s1, *sptr;

sptr = &s1;

Now, sptr points to s1 and can be used to access member variable of struct student.

To access member variables using pointer to structure, we have to use an arrow -> operator.
Syntax:
            struct_pointer -> member_variable;
Here, struct_pointer is a pointer to structure, and member_variable is the name of member variable.


Example:
            s1 -> rollno = 20;

We can also use dot (.) operator to access member variable with pointer to structure like:
            (*s1).rollno = 20;


Difference between Structure and Union

Structure
Union
1. The keyword  struct is used to define a structure
1. The keyword  union is used to define a structure
2. In structure, each member has its own separate space.
2. In union, all members share the storage space.
3. Memory occupied is the total required to store all the members.
3. Memory occupied is only that much which is required to store the largest member of union.
4. All members can be accessed at anytime.                                                      
4. Only one member can be activated at a time.
5. Syntaxt:
struct strcture_name
      {
         datatype-1 variable-1;
         datatype-2 variable-2;
         datatype-3 variable-3;
           }

5. Syntaxt:
union strcture_name
      {
         datatype-1 variable-1;
         datatype-2 variable-2;
         datatype-3 variable-3;
    }
6. Example:
struct std
      {
          int rollno;
          char name[20];
          float salary;
      }
Total Size required  = 2 + 20+ 4
                                 = 26
6. Example:
union std
      {
          int rollno;
          char name[20];
          float salary;
      }
Total Size required  = max( 2, 20, 4)
                                 = 20