Showing posts with label Structure. Show all posts
Showing posts with label Structure. Show all posts

Thursday 4 August 2016

Union within Structure

Union within Structure



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
            int marks;
            union
            {
                        char name[20];
                        int usn;
            } u;
} s;
main()
{
            int  ch;
            printf("\n Enter marks:");
            scanf("%d", &s.marks);
            printf("\nName or USN choice:");
            printf("\n 1.Name 2.USN: ");
            scanf("%d", &ch);
            switch(ch)
            {
                        case 1:             printf("\nEnter name:");
                                                scanf("%s", s.u.name);
                                                printf("\nYour name is:%s", s.u.name);
                                                break;
                        case 2:             printf("\nEnter usn:");
                                                scanf("%d", &s.u.usn);
                                                printf("\nYour usn is:%d", s.u.usn);
                                                break;
            }
            printf("\nYour marks is:%d", s.marks);
}
Output:
Enter marks: 80
Name or USN choice:
1.Name 2.USN:            1
Enter name: AAAAA
Your name is: AAAAA

Your marks is: 80 

Structure and Union Initialization

Structure and Union Initialization


#include<stdio.h>
struct first
{
            int x, y;
};

union second
{
            int x, y;
};
main()
{
            struct first f1 = {1, 2};
            printf("\nf1.x = %d \t f1.y = %d", f1.x, f1.y);

            union second s1;
            s1.x = 3;
            printf("\ns1.x = %d\t", s1.x);
            s1.y = 4;
            printf("\ns1.y = %d", s1.y);

            union second s2 = {6, 7};
            printf("\ns2.x = %d\t s2.y = %d", s2.x, s2.y);

            union second s3;
            s3.x = 10;
            s3.y = 9;
            printf("\ns3.x = %d\t s3.y = %d", s3.x, s3.y);
}
Output:
f1.x = 1   f1.y = 2
s1.x = 3   s1.y = 4
s2.x = 6   s2.y = 6
s3.x = 9   s3.y = 9

Unions

The Size and Address of Unions

The Size and Address of Structure and Unions

#include<stdio.h>
#include<stdlib.h>

struct stag
{
            char c;
            int i;
            float f;
            double d;
};
union utag
{
            char c;
            int i;
            float f;
            double d;
};
main()
{
            struct stag s;
            printf("\n size of structure=%u", sizeof(struct stag));
            printf("\n size of structure=%u", sizeof(s));
            printf("\n address of structure=%u", &s);
            printf("\n address of structure members: %u %u %u %u", &s.c, &s.i, &s.f, &s.d);

            union utag u;
            printf("\n size of union=%u",sizeof(union utag));
            printf("\n size of union=%u",sizeof(u));
            printf("\n address of union=%u",&u);
            printf("\n address of union members: %u %u %u %u",&u.c,&u.i,&u.f,&u.d);
}

Output:
 size of structure=24
 size of structure=24
 address of structure=2686752
 address of structure members: 2686752      2686756          2686760          2686768

 size of union=8
 size of union=8
 address of union=2686744
 address of union members: 2686744            2686744          2686744          2686744

Saturday 30 July 2016

Passing Address of Structure Variable to Function

Passing Address of Structure Variable to Function


#include<stdio.h>

void display(struct student *p);

struct student
{
            char name[20];
            int rollno;
            float mark;
};

int main()
{
            struct student S1;
            printf("\nEnter name rollno and mark of a student:");
            scanf("%s %d %f", S1.name, &S1.rollno, &S1.mark );
            display(&S1);
}

void display(struct student *p)
{
    printf("\n Name is %s \n Rollno is %d \n Mark is %f", p->name, p->rollno, p->mark );
}

Output:
Enter name rollno and mark of a student: aaa 11 80
 Name is aaa
 Rollno is 11
 Mark is 80.000000

Passing Structure Variable to Function

Passing Structure Variable to Function


#include<stdio.h>
void display(struct student);
struct student
{
            char name[20];           int rollno;        float mark;
};

void main()
{
            struct student S1;
            printf("\nEnter name rollno and mark of a student:");
            scanf("%s %d %f", S1.name, &S1.rollno, &S1.mark );
            display(S1);
}

void display(struct student stu)
{
            printf("\n Name is %s \n Rollno is %d \n Mark is %f", stu.name, stu.rollno, stu.mark );
}

Output:

Enter name rollno and mark of a student: aaa 11 80
 Name is aaa
 Rollno is 11
 Mark is 80.000000

Passing Individual Structure Members to Function

Passing Individual Structure Members to Function


#include<stdio.h>
void display(char nam[], int roll, float mrk);

struct student
{
            char name[20];
            int rollno;
            float mark;
};
main()
{
            struct student S1;
            printf("\nEnter name rollno and mark of a student:");
            scanf("%s %d %f", S1.name, &S1.rollno, &S1.mark );
            display(S1.name, S1.rollno, S1.mark );
}
void display(char nam[], int roll, float mrk)
{
            printf("\n Name is %s \n Rollno is %d \n Mark is %f", nam, roll, mrk );
}

Output:
Enter name rollno and mark of a student:     aaa          11        80
 Name is aaa
 Rollno is 11
 Mark is 80.000000


Goto Main Content:
https://tejaswinihbhat.blogspot.in/2016/07/data-structures-15CS33-Module1.html
https://tejaswinihbhat.blogspot.in/2016/07/structures.html

Structures and Pointers

Structures and Pointers

#include<stdio.h>
struct student
{
            char name[20];
            int rollno;
            float mark;
};
main()
{
            struct student   S1;
            struct student   *p;
            p = &S1;
            printf("\nEnter name rollno and mark of a student:");
            scanf("%s %d %f", p->name, &p->rollno, &p->mark );
            printf("\n Name is %s \n Rollno is %d \n Mark is %f", p->name, p->rollno, p->mark );
}

Output:
Enter name rollno and mark of a student: aaa 21 98
Name is aaa
Rollno is 21
Mark is 98.000000

Array of Structures

Array of Structures

#include<stdio.h>
struct student
{
            char name[20];          
            int rollno;           
            float mark;
};
main()
{
            struct student S[10];
            int i, n;
            printf("\nEnter the number of students: ");
            scanf("%d", &n);
            for(i=0; i<n; i++)
            {
                    printf("\nEnter name rollno and mark of a student %d: ", i+1);
                        scanf("%s %d %f", S[i].name, &S[i].rollno, &S[i].mark );
            }
            for(i=0; i<n; i++)
            {
                        printf("\nStudent %d details: ", i+1);
                        printf("\n \tName is %s \n\tRollno is %d \n\tMark is %f", S[i].name, S[i].rollno, S[i].mark );
            }
}

Output:
Enter the number of students: 3


Enter name rollno and mark of a student 1: aaa       10        55
Enter name rollno and mark of a student 2: bbb       20        66
Enter name rollno and mark of a student 3: ccc        30        77
Student 1 details:
    Name is aaa
    Rollno is 10
    Mark is 55.000000
Student 2 details:
    Name is bbb
    Rollno is 20
    Mark is 66.000000
Student 3 details:
    Name is ccc
    Rollno is 30
    Mark is 77.000000


Goto Main Content:
https://tejaswinihbhat.blogspot.in/2016/07/data-structures-15CS33-Module1.html
https://tejaswinihbhat.blogspot.in/2016/07/structures.html


Thursday 28 July 2016

Initialization of Structure Variables

Structure Variable Initialization


#include<stdio.h>
struct student
{
char name[20];
    int rollno;
    float mark;
}S1={"aaaa", 111, 40};                                          // Method 1 


void main()
{
     struct student S2 = {"BBB", 222, 50};         // Method 2

    struct student S3, S4, S5;

    printf("\n\n Student1: \n\tName is %s \n\tRollno is %d \n\tMark is %f", S1.name, S1.rollno,  S1.mark );
    printf("\n\n Student2: \n\tName is %s \n\tRollno is %d \n\tMark is %f", S2.name, S2.rollno, S2.mark );

    S3 = S2;                // Method 3: Assigning one structure variable to another
    printf("\n\n Student3: \n\tName is %s \n\tRollno is %d \n\tMark is %f", S3.name, S3.rollno, S3.mark );

         // Method 4: Individual members initialization
          strcpy(S4.name, "DDD");
          S4.rollno = 444;
          S4.mark = 70;
          printf("\n\n Student4: \n\tName is %s \n\tRollno is %d \n\tMark is %f", S4.name, S4.rollno, S4.mark );

         // Method 5: Taking values from user
    printf("\nEnter name rollno and mark of a student5:");
    scanf("%s %d %f", S5.name, &S5.rollno, &S5.mark );
    printf("\n\n Student5: \n\tName is %s \n\tRollno is %d \n\tMark is %f", S5.name, S5.rollno, S5.mark );

}

Output:

 Student1:
        Name is aaaa
        Rollno is 111
        Mark is 40.000000

 Student2:
        Name is BBB
        Rollno is 222
        Mark is 50.000000

 Student3:
        Name is BBB
        Rollno is 222
        Mark is 50.000000

 Student4:
        Name is DDD
        Rollno is 444
        Mark is 70.000000

Enter name rollno and mark of a student5:       EEE       555          70
Student5:
        Name is EEE
        Rollno is 555
        Mark is 70.000000


Goto Main Content:
https://tejaswinihbhat.blogspot.in/2016/07/data-structures-15CS33-Module1.html
https://tejaswinihbhat.blogspot.in/2016/07/structures.html

Declaring Structure Variables

Structure Variable Declaration


1.  With structure definition

    Syntax:

     struct   tagname

    {
          datatype member1;
          datatype member2;
          :
          :
          datatype memberN;
     } var1, var2, var3 ;

     Example:



     struct  student
     {
            char  name[20];
            char usn[20];
            int age;
            float mark;
     } S1, S2, S3;  

---------------------------------------------------------------------------------------------------------------------

2.  Omitting tagname

    Syntax:


   struct   

   {
          datatype member1;
          datatype member2;
          :
          :
          datatype memberN;
   } var1, var2, var3 ;


   Example:



   struct  
   {
           char  name[20];
           char usn[20];
           int age;
           float mark;
   }S1, S2, S3;  
  
---------------------------------------------------------------------------------------------------------------------

3. Using struct tagname

    Syntax:

    struct   tagname
   {
          datatype member1;
          datatype member2;
          :
          :
          datatype memberN;
   };

   struct tagname var1, var2, var3 ;


   Example:

   struct  student
   {
         char  name[20];
         char usn[20];
         int age;
         float mark;
    };

    struct student S1, S2, S3;  

---------------------------------------------------------------------------------------------------------------------

4. Using typedef

    Syntax:

    struct   tagname
   {
          datatype member1;
          datatype member2;
          :
          :
          datatype memberN;
   };

   typedef struct tagname newname;
   newname var1, var2, var3 ;

  Example:

   struct  student
   {
         char  name[20];
         char usn[20];
         int age;
         float mark;
    };

   typedef struct student stu;
   stu S1, S2, S3;  

---------------------------------------------------------------------------------------------------------------------

5. Using typedef struct tagname

    Syntax:

   typedef struct   tagname
   {
          datatype member1;
          datatype member2;
          :
          :
          datatype memberN;
   }newname;

   newname var1, var2, var3 ;

  Example:

   typedef struct student
   {
         char  name[20];
         char usn[20];
         int age;
         float mark;
    }stu;

   stu S1, S2, S3;  

---------------------------------------------------------------------------------------------------------------------

6. Using typedef struct 

    Syntax:

   typedef struct 
   {
          datatype member1;
          datatype member2;
          :
          :
          datatype memberN;
   }newname;

   newname var1, var2, var3 ;

  Example:

   typedef struct 
   {
         char  name[20];
         char usn[20];
         int age;
         float mark;
    }stu;

   stu S1, S2, S3;  

---------------------------------------------------------------------------------------------------------------------

7. Using typedef struct tagname (omiting newname)

    Syntax:

   typedef struct tagname
   {
          datatype member1;
          datatype member2;
          :
          :
          datatype memberN;
   };

   tagname   var1, var2, var3 ;

  Example:

   typedef struct student
   {
         char  name[20];
         char usn[20];
         int age;
         float mark;
    };

   student S1, S2, S3;  

---------------------------------------------------------------------------------------------------------------------



-->Check  Next Topic-->
https://tejaswinihbhat.blogspot.in/2016/07/initialization-of-structure-variables.html

Goto Main Content:


https://tejaswinihbhat.blogspot.in/2016/07/data-structures-15CS33-Module1.html

https://tejaswinihbhat.blogspot.in/2016/07/structures.html