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 Within the Structure

Array Within the Structure


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

}

Output:
Enter the number of students: 2

Enter Student 1 details:
Enter name rollno: aaa 11
Enter 3 marks: 50 60 70

Enter Student 2 details:
Enter name rollno: bbb 22
Enter 3 marks: 60 70 80

Student 1 details:
        Name is aaa
        Rollno is 11
        Marks are: 50.000000    60.000000       70.000000
Student 2 details:
        Name is bbb
        Rollno is 22
        Marks are: 60.000000    70.000000       80.000000

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

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


Structure Template

Structure

  • Structure is a collection of elements of different data type under a single name.


Structure Template:

  • A structure is declared using the keyword struct followed by the structure name (tagname).

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


Here,
  • Declaring a structure creates a template that describes the characteristics of its members.
  • struct  is a keyword.
  • tagname is the structure name.
  • Here struct tagname becomes the datatype.
  • member1, member2,.... memberN are the members of the structure.
  • So, structure variables can be declared using struct tagname.
  • The elements of a structure are referred as members.
  • Every structure template should end with semicolon.
  • Declaration of the structure template does not reserve any space in memory for the members; space is reserved only when variables of this structure type are declared.
  • Members are not variables; they don't have any existence till they are attached with a structure variable.
  • Member names inside a structure should be different from one another; but these names can be similar to any other variable name declared outside the structure.
  • Member names inside 2 different structures can also be same.
  • Structure template can be declared gloably / locally. 

Example:

#include<stdio.h>
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 );
            printf("\n Name is %s \n Rollno is %d \n Mark is %f", S1.name, S1.rollno, S1.mark );
}

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

Structures