C Structures

Introduction to Structure

  • Structure is a user defined data type in C Language.
  • It is use to define new data type by using existing implicit data type in C.
  • Structure is also known as group of dissimilar/different data types, which has the common name.
  • It is used to construct complex data type which is meaningful to represent real world objects.
  • struct keyword is use to create new structure.

IN RUSH ? START WITH EXAMPLES

  1. STRUCTURE EXAMPLE
  2. ARRAY OF STRUCTURE EXAMPLE
  3. POINTER TO STRUCTURE EXAMPLE
  4. STRUCTURE WITHIN STRUCTURE EXAMPLE (NESTED STRUCTURE)

HAVE PATIENCE ? START SYSTEMATICALLY

Syntax to create structure:

struct structureName
{
  dataType fieldNeme1;
  dataType fieldName2;
  ...
};

SO, LETS CREATE A STRUCTURE

struct Student
{
	int no;
	char name[20];
	float perc;
};

Here, student structure OR user defined data type OR derived data type is created. Now we can declare the variable of student data type.

LETS DECLARE STRUCTURE VARIABLE

struct Student
{
	int no;
	char name[20];
	float perc;
};

void main()
{
	struct Student s1, s2, s[5];
}

Another way to declare structure variable

struct Student
{
	int no;
	char name[20];
	float perc;
}s1, s2, s[5];

In both the cases

  • s1 and s2 are structure variable,
  • s[5] is an array of structure of size 5.

LETS, SEE HOW TO ACCESS MEMBERS/FIELDS OF STRUCTURE

  • member operator . is used to access members/fields of a structure variable.

1. STRUCTURE EXAMPLE

# include <stdio.h>
# include <conio.h>

// author : www.raviroza.com
// date : 8-Dec-2021, 8.00 am

struct Student
{
	int no;
	char name[20];
	float perc;
};

void main()
{
	struct Student s1;
	
	fflush(stdin);
	printf ("enter Student number : ");
	scanf  ("%d",&s1.no);
	
	fflush(stdin);
	printf ("enter Student Name : ");
	scanf  ("%s",s1.name);
	
	fflush(stdin);
	printf ("enter Student percentage : ");
	scanf  ("%f",&s1.perc);
	
	printf ("\nStudent Summary\n\n");
	printf ("No.        : %d\n",s1.no);
	printf ("Name       : %s\n",s1.name);
	printf ("Percentage : %f\n",s1.perc);
		
	getch();	

}

2. ARRAY OF STRUCTURE EXAMPLE

# include <stdio.h>
# include <conio.h>


// author : www.raviroza.com
// date : 8-Dec-2021, 9.45 am

struct Student
{
	int no;
	char name[20];
	float perc;
};

void main()
{
	struct Student stu[5];
	int i, n;
	
	printf ("enter no. of student : ");
	scanf  ("%d",&n);
	
	for (i=0; i<n; i++)
	{
		fflush(stdin);
		printf ("enter Student number : ");
		scanf  ("%d",&stu[i].no);
	
	
		fflush(stdin);
		printf ("enter Student Name : ");
		scanf  ("%s",stu[i].name);
	
		fflush(stdin);
		printf ("enter Student percentage : ");
		scanf  ("%f",&stu[i].perc);
	}
	
	printf ("\nStudent Summary\n");
	printf ("\n----------------------------\n");
	for (i=0; i<n; i++)
	{			
		printf ("No.        : %d\n",stu[i].no);
		printf ("Name       : %s\n",stu[i].name);
		printf ("Percentage : %f\n",stu[i].perc);
		printf ("\n----------------------------\n");
	}	
	getch();	
}

3. POINTER TO STRUCTURE EXAMPLE


# include <stdio.h>
# include <conio.h>

// author : www.raviroza.com
// date : 20-Dec-2021, 8.00 am

struct Student
{
	int no;
	char name[20];

};

void main()
{
    struct Student *sptr, stu1;
    sptr = &stu1;

    printf("Enter Student No. : ");
    scanf("%d", &sptr->no);

    printf("Enter Student Name : ");
    scanf("%s", &sptr->name);

    printf("Student Details:\n");
    printf("No.    : %d\n", sptr->no);
    printf("Name   : %s",   sptr->name);

    getch();
}

4. STRUCTURE WITHIN STRUCTURE


# include <stdio.h>
# include <conio.h>

// author : www.raviroza.com
// date : 20-Dec-2021, 8.30 am

struct student
{
    int no;
    char *name;
    struct marks
    {
        int m1,m2,m3;
    }mark;
    
};

// typedef is used to create the alias of data types

typedef struct student stu;

void main()
{
    //struct student s;
    stu s;
    s.no=101;
    s.name = "ravi r oza";
    s.mark.m1=75;
    printf ("%d %s %d",s.no,s.name,s.mark.m1);
    getch();
}